Reputation: 179
I have following CSV file:
a,b,c,d
x,1,1,1
y,1,1,0
z,1,0,0
I want to keep lines that add up more than 1, so I execute this awk command:
awk -F "," 'NR > 1{s=0; for (i=2;i<=NF;i++) s+=$i; if (s>1)print}' file
And obtain this:
x,1,1,1
y,1,1,0
How can I do the same but retain the first line (header)?
Upvotes: 6
Views: 10686
Reputation: 37404
Since it's only 0s and 1s:
$ awk 'NR==1 || gsub(/1/, "1") > 1' file
a,b,c,d
x,1,1,1
y,1,1,0
Upvotes: 1
Reputation: 10865
$ awk -F "," 'NR==1; NR > 1{s=0; for (i=2;i<=NF;i++) s+=$i; if (s>1)print}' file
a,b,c,d
x,1,1,1
y,1,1,0
Upvotes: 13