Skip to content
Advertisement

Awk: How do I count occurrences of a string across columns and find the maximum across rows?

I have a problem with my bash script on Linux.

My input looks like this:

  input
    Karydhs     y n y y y n n y n n n y n y n 

    Markopoulos y y n n n y n y n y y n n n y

    name3       y n y n n n n n y y n y n y n

etc…

which y=yes and n=no and that is the results of voting… and now I want with using awk to display the name and the total yes vote of each person (name) and the person that win (got the most y), any ideas?

I do something like this:

awk '{count=0 for (I=1;i<=15;i++) if (a[I]="y") count++} {print $1,count}' filename

Advertisement

Answer

What about this?

awk '{ for (i=2;i<NF;i++) { if ($i=="y") { a[$1" "$i]++} } } END { print "Yes tally"; l=0; for (i in a) { print i,a[i]; if (l>a[i]) { l=l } else { l=a[i];name=i }   } split(name,a," "); print "Winner is ",a[1],"with ",l,"votes"  } ' f
Yes tally
name3 y 6
Markopoulos y 6
Karydhs y 7
Winner is  Karydhs with  7 votes
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement