0

I need to collect the lines where at least one value in the columns A, B, D, E is not equal to 0 (zero).

Below one is the data:

                   A      B     C           D        E       G       
ES-N    .    |     0      0     12  |       0        0      100
SES-N   .    |     1      0     10  |       0        0       10
UAS-N   .    |     0      0     10  |       0        0       10
CSS-N   .    |     0      0     10  |       0        5       10
BBE-N   .    |     0      0     15  |       0        0      125

Required output:

                   A      B     C           D        E       G       
SES-N   .    |     1      0     10  |       0        0       10
CSS-N   .    |     0      0     10  |       0        5       10

Could you please provide a shell script which can work in unix platform.

I did below but not eliminated the "0" because of C and G column.

#lin is the count of line in the file
for  (( i=1; i<=$lin; i++ ));
do
sed -n -e ""$((ini + 1)),$fin"p"  /file
egrep '1|2|3|4|5|6|7|8|9' /file1
if [[ $? -eq 0 ]]
then
sed -n -e ""$ini"p" /file
1
  • 1
    Do you mean you need to collect the lines where A AND B AND D AND E value is not equal to 0 (zero). or to collect the lines where A OR B OR D OR E value is not equal to 0 (zero). – francois P Sep 11 '19 at 20:38
5

Using awk:

awk -n '$4 !=0 || $5 != 0 || $8 != 0 || $9 != 0 {print $0}' filename
0

simply you can also do:

awk '$4+$5+$8+$9' infile

and to keep header line:

awk 'NR==1 || $4+$5+$8+$9' infile
0

@unxnut's awk solution is more flexible, but in your particular case, because your requirements are simple, and the file format is also simple, grep can do this as well.

To include lines where at least one value is non-zero, is the same thing as to exclude lines where all of those values are zero. This grep command looks for lines that contain two instances of:

  • vertical bar followed by whitespace
  • zero followed by whitespace
  • another zero followed by whitespace

Any line that matches two instances of that pattern (that is, any line where all four values are zero) is removed. The remaining lines are displayed:

$ egrep -v '(\|  *0  *0 .*){2}' infile
                   A      B     C           D        E       G       
SES-N   .    |     1      0     10  |       0        0       10
CSS-N   .    |     0      0     10  |       0        5       10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.