Awk: how to filter a row where all records (16 columns) are 0

My CSV file is divided by tab, and I want to filter those rows that have all the entries (in 16 columns) of 0. I am doing this now

awk '$1 != 0 && $2 != 0 && ....omitted && $16 != 0 {print $0}' file.csv > newFile.csv

As you can see, it is so tedious to enter the same conditions for all 16 columns. Is there an easier way?

+4
source share
3 answers

How about something like that (assuming you have 16 fields)

awk '{for (i=1; i<=16; ++i) if($i != 0) {print;next}}' file.csv > newFile.csv
+4
source

Sort of

grep -Ev '^0+([[:space:]]+0+){15}$' file.csv

or

awk --posix '!/^0+([[:space:]]+0+){15}/'
+5
source

sed -n '/[^0\t]/p' file.csv

, 0 \t (tab). :

sed '/[^0\t]/!d' file.csv

, (!), [^0\t].

+3
source

All Articles