Chudar Chudar - 4 months ago 8
Linux Question

Filtering rows in a text file using awk

I have tab delimted txt file löike follows:

A 1 A1g DELL
A 1 B1s HP
A 7 Zfd Nog
A 5 Jgf KIT
A 1 Def JOP


Except first row, I would like to retain only those rows in which second column has value >1.

A 1 A1g DELL
A 7 Zfd Nog
A 5 Jgf KIT


I tried
awk '{if($2>1) print }' file.txt
but it omits the first row also. How can I check skip the first row and check the condition
($2 >1)
for other rows. Thanks in advance.

Answer

In AWK:

$ awk 'NR==1||$2>1' test.in
A  1 A1g DELL
A  7 Zfd Nog
A  5 Jgf KIT