Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete an entire row if a specific column contains a zero?

Tags:

sed

awk

perl

I need to delete the rows which contains 0 value in column number 5.

Before:

1QWO     10      5     45    100    7.5
1R0R      6      3     15    100    8.5
1R5M      4      0      6      0    6.5
1R8S      4      0      6      0      6
1R9L      2      1      1    100    6.3
1RG8      6      0     15      0    5.8
1RJU     10      0     45      0    7.2
1RKI     12      6     66    100    4.6

After:

1QWO     10      5     45    100    7.5
1R0R      6      3     15    100    8.5
1R9L      2      1      1    100    6.3
1RKI     12      6     66    100    4.6

Any one liner will be very helpful.

I know:

sed '/0/d' file_name

but above command will delete 0 from all the columns.

like image 847
user2176228 Avatar asked Nov 28 '22 14:11

user2176228


2 Answers

awk '$5 != 0'

The machine won't let me submit unless I add this extra text.

like image 187
John Zwinck Avatar answered Dec 06 '22 19:12

John Zwinck


Even faster:

awk '$5' file

If the 5th field is not 0 then the condition is evaluated as true - note that {print $0} is the default block in awk so it can be omitted if this is the only action you want to perform.

like image 30
fedorqui 'SO stop harming' Avatar answered Dec 06 '22 20:12

fedorqui 'SO stop harming'