awk '{print "line #", NR, "-", gsub("0/0",""), gsub("0/1",""), gsub("1/1","")}' file
counting the occurrences of a field on a individual line
Pergunta
I am trying to take a file that has many 1000's of lines, and for each lines I would like to count the occurrences that the individuals fields occur. So an example of a few lines of the file look like this:
0/0 0/0 0/0 0/0 0/0 0/0 0/1 0/0 0/0 1/1 0/0 0/1
0/1 1/1 0/1 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0
my code is this:
cat file | awk 'BEGIN{a=0; c=0; g=0;} {a+=gsub("0/0",""); c+=gsub("0/1",""); g+=gsub("1/1","")} END{print a,c,g}'
The output that I get is:
18 4 2
The output that I want is:
line #1- 9 2 1
line #2- 9 2 1
I can't seem to figure out what I am doing wrong
Solução
Outras dicas
With GNU awk
you can try something like this (The output can be random. Since you haven't mentioned the order you'd like to get, I am assuming this should be ok):
awk '{delete a; printf "line #%s- ", NR; for(x=1;x<=NF;x++) a[$x]++; for(y in a) printf "%s ", a[y]; print ""}' file
Output:
$ cat file
0/0 0/0 0/0 0/0 0/0 0/0 0/1 0/0 0/0 1/1 0/0 0/1
0/1 1/1 0/1 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0
$ awk '{delete a; printf "line #%s- ", NR; for(x=1;x<=NF;x++) a[$x]++; for(y in a) printf "%s ", a[y]; print ""}' file
line #1- 9 2 1
line #2- 9 2 1
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow