Remove from a file all lines with value equal to 0

Dear All,
I have a file containing 1134 columns and 20825 rows,
tabulated as follow

	-4000	-3900	-3800	-3700	-3600	-3500	-3400
NR_033530	0	0	0	0	0	0	0
NM_001162375	0	0	0	0	0	0	0
NM_007669	0	0	0	0	0	0	328,98
NM_008104	0	388,94	388,94	388,94	0	0	0
NM_010472	0	0	0	0	0	0	0
NM_001110205	0	0	0	0	0	0	0
NM_001110204	0	0	0	0	98,76	0	0
NM_001110193	0	0	0	0	0	0	0
NM_019747	0	0	0	0	0	0	0
NM_021338	328,98	0	0	0	0	0	0
NR_029560	0	0	0	0	0	0	0
NR_004853	0	0	0	0	388,94	388,94	388,94
NM_008164	0	0	0	0	0	0	0
NM_008162	0	0	0	0	0	0	0
NR_027664	0	0	0	0	0	0	0
NM_175091	0	0	0	0	0	0	0
NM_173770	0	388,94	388,94	388,94	0	0	0
NM_178045	0	0	0	0	0	0	0
NM_178028	0	0	0	0	0	0	0
NM_177909	0	0	98,76	0	0	0	0
NM_177638	0	0	0	0	0	0	0
NM_177633	0	328,98	0	0	0	0	0
NM_178757	0	0	0	0	2	0	0

I would like to remove all lines with all values equal to 0, to achieve a file like this:

	-4000	-3900	-3800	-3700	-3600	-3500	-3400
NM_021338	328,98	0	0	0	0	0	0
NM_177633	0	328,98	0	0	0	0	0
NM_177909	0	0	98,76	0	0	0	0
NM_008104	0	388,94	388,94	388,94	0	0	0
NM_173770	0	388,94	388,94	388,94	0	0	0
NM_178757	0	0	0	0	2	0	0
NM_001110204	0	0	0	0	98,76	0	0
NM_007669	0	0	0	0	0	0	328,98
NR_004853	0	0	0	0	388,94	388,94	388,94

Any suggestion?

Many many thanks,
Paolo

Hello,

with grep:

grep -v '^[^[:space:]]*\([[:space:]]*0\)*$' file

Regards.

1 Like

Many many thanks!
Paolo

Or, a bit simpler:

 grep -v '^[^[:space:]]*[[:space:]0]*$' file

Using awk

awk -F\\t '{for (i=2;i<=NF;i++) a+=$i} a{print;a=0}' file

But I do like the regex approach, since no loops are involved

awk '!/^[^[:space:]]*[[:space:]0]*$/' file

Using patterns may not work reliably, because there are many ways to represent a zero value, for example .0 00 0.0 +0.0 , etc.. So an approach like Jotne's would be preferable, but the method of addition will fail if there are non-zero values that add up to 0.

But something like this should work:

awk '{for (i=2;i<=NF;i++) if($i){print; next}}'  file

It's right, so just change the regular expression to search for a nonzero number after first "space" separator:

grep '[[:space:]].*[1-9]' file

Regards

2 Likes

Nice find :b: