saamar rajput
saamar rajput

Reputation: 75

How to delete rows depending on more than 1 column conditions

I have a file with 12 columns, and i want to delete rows that have 0 value at columns 4,5,6,7,8,9,10,11 and 12 at the same time.

I wrote this code but it doesn't work:

cat Combined_PromoterAnalysis.bed |\
 awk -F "" '{ if ($4==0,$5==0,$6==0,$7==0,$8==0,$9==0,$10==0,$11==0,$12==0)  print $0 }' > Modified.bed 

Upvotes: 1

Views: 41

Answers (1)

fedorqui
fedorqui

Reputation: 289515

You need to check that everything is true. For this, you need to say condition1 && condition2 ....

This will print all those lines in which at least one of these files is not 0:

awk -F "" '!($4==0 && $5==0 && $6==0 && $7==0 && $8==0 && $9==0 && $10==0 && $11==0 && $12==0)' file

However, it may be better to loop through these fields and print the line whenever one of them is not 0:

awk '{for (i=4;i<=12;i++) if ($i!=0) {print; next}}' file

Can the values be negative? Because if they are all positive you can check if the sum is 0:

awk -F "" '$4+$5+$6+$7+$8+$9+$10+$11+$12' file

Upvotes: 3

Related Questions