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

I need to delete rows containing 0 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 line will be very useful.

I know:

 sed '/0/d' file_name 

but the above command will remove 0 from all columns.

+4
source share
4 answers

With portable sed, you can do it like this:

 sed '/\([^ ]\+ \+\)\{4\}0\+ /d' 

This way you limit the zero check to the fifth column.

With perl, you can use automatic separation, note that you do not need to specify a condition for $F[4] , because it is implicit:

 perl -ane 'print if $F[4]' 

The conclusion in both cases:

 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 
+3
source
 awk '$5 != 0' 

The machine will not allow me to send unless I add this additional text.

+9
source

Even faster:

 awk '$5' file 

If the fifth field is not 0 , then the condition evaluates to true - note that {print $0} is the default block in awk , so you can omit it if this is the only action you want to perform.

+8
source

This is more suitable for awk :

 $ awk '$5!=0' file 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 

But if you really want a sed solution:

 $ sed -r '/\S+\s+{4}0\s+/d' file 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 
+6
source