Calculate proportion of specific letters in a file using AWK

Viewed 79

First of all, thank you for helping with my problem. I have a csv file where I have a column that have different letter encoded, as you can see here.

ABC
CD
EF
F
D
F
AS

I know how to calculate the proportion of F in the column and display the output as follows:

awk 'BEGIN{sum=0;count=0}{count++}{if($1="F")sum+=$1}END {print sum/count}'
0.286

But my problem is that I want to do it generally with every single line of the column that have one character so the output will be:

F 0.286
D 0.143

Thank you everyone for your help.

2 Answers

Instead of using if ($1=="F") to filter out a specific character, store all single letters in an array and iterate over them at the end.

awk 'length($0)==1{a[$0]++} END{for(c in a) print c, a[c]/NR}'

Arrays in awk have no fixed iteration order, so the output might be either

F 0.286
D 0.143

or

D 0.143
F 0.286

If you want a fixed order, pipe the result through sort.

With your shown samples, could you please try following.

awk '
/^.$/{
  count[$0]++
}
END{
  for(key in count){
    print key,count[key]/FNR
  }
}
' Input_file

Explanation: Adding detailed explanation for above.

awk '                    ##Starting awk program from here.
/^.$/{                   ##Checking condition if line is having 1 character long.
  count[$0]++            ##Creating count with index of current line and keep increasing count of it with 1 here.
}
END{                     ##Starting END block of this program from here.
  for(key in count){     ##Traversing through count array here.
    print key,count[key]  ##printing key and printing divide of value of count with FNR.
  }
}
' Input_file             ##Mentioning Input_file name here.
Related