Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk: how to filter out a row that all entries (16 columns) are 0

Tags:

csv

filter

awk

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

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

As you can see this is so tiring for inputing the same conditions for all 16 columns. Is there any easier way?

like image 272
TonyGW Avatar asked Dec 16 '22 04:12

TonyGW


2 Answers

What about something like

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

or

awk --posix '!/^0+([[:space:]]+0+){15}/'
like image 120
Etan Reisner Avatar answered Feb 16 '23 14:02

Etan Reisner


How about something like this (assuming you have 16 fields throughout)

awk '{for (i=1; i<=16; ++i) if($i != 0) {print;next}}' file.csv > newFile.csv
like image 35
iruvar Avatar answered Feb 16 '23 14:02

iruvar