AishwaryaKulkarni AishwaryaKulkarni - 5 months ago 16
Bash Question

Count unique numbers in a line of a matrix

I have a matrix:

>ACTTT ASB 0.098
0 0 1 0
0.75 0 0.25 0
0 0 0 1
0 1 0 0
1 0 0 0
1 0 0 0
0 1 0 0
0 1 0 0


And I want to count the occurrence of '0's not '0.' in the file per line (while ignoring the header), such that for each line I will get the count as 3,2,3,3,3,3,3.
I tried echo $line | grep -o 0 | wc -l , but it counts the '0.' as well.

Answer
$ awk 'NR>1{print gsub(/(^|[[:space:]])0([[:space:]]|$)/,"&")}' file
3
2
3
3
3
3
3
3
Comments