Yes, you can do this in awk:
awk '{ k=0; for(i=2;i<=NF;i++){ if($i == 0){ k++ } } if(k==3){ tot++ } } END{ print tot }' file But, personally, I would do in in perl instead:
$ perl -ale '$tot++ if (grep{$_ == 0 } @F) == 3 }{ print $tot' file 7 Or, the slightly less condensed:
$ perl -ale 'if( (grep{$_ == 0 } @F) == 3 ){ $tot++ } END{ print $tot }' file 7 And the same thing, for the golfers among you:
$ perl -ale '(grep{$_==0}@F)==3&&$t++}{print$t' file 7 Explanation
-ale:-amakes perl behave like awk. It will read each line of the input file and split it on whitespace into the array@F. The-ladds a\nto each call ofprintand removes trailing newlines from the input and the-eis the script that should be applied to each line of input.$tot++ if (grep{$_ == 0 } @F) == 3: increment$totby one, for every time where there are exactly 3 fields that are0. Since the 1st field starts from 1, we know it will never be 0 so we don't need to exclude it.}{: this is just a shorthand way of writingEND{}, of giving a block of code that will be executed after the file has been processed. So,}{ print $totwill print the total number of lines with exactly three fields with a value of0.