I have a file
that looks like this :
header,d0,d1,d2,d3, ...
s1,0,5,2,8, ...
s2,0,8,2,4, ...
s3,0,7,3,4, ...
s4,0,3,2,1, ...
...
I want to remove any column with all zeros like d0
I can manually inspect for columns with all zeros and find d0 and execute
cut -d "," -f 1,3- file> file_revised
The desired output is
header,d1,d2,d3, ...
s1,5,2,8, ...
s2,8,2,4, ...
s3,7,3,4, ...
s4,3,2,1, ...
...
But since I have so many columns, it is hard to inspect manually.
How can I automatically remove columns with all zeros?
Thank you.
$ cat file
header,d0,d1,d2,d3
s1,0,5,2,8
s2,0,8,2,4
s3,0,7,3,4
s4,0,3,2,1
$
$ cat tst.awk
NR==1 {
for (i=1; i<=NF; ++i)
a[i]
next
}
NR==FNR {
for (i in a)
if ($i != "0")
delete a[i]
next
}
{
sep = ""
out = ""
for (i=1; i<=NF; ++i) {
if (i in a)
continue
out = out sep $i
sep = FS
}
print out
}
$
$ awk -F, -f tst.awk file file
header,d1,d2,d3
s1,5,2,8
s2,8,2,4
s3,7,3,4
s4,3,2,1