Text Processing with awk – Calculate Average of Values Based on Another Field

awktext processing

Is there a way to get average of values in a field based on variables in another field? For example for the following input

a x 3
b y 4
a y 2
b x 5
b x 20

I want this output

a 2.5
b 9.67

I found this awk script to get average for values in a column

awk '{ total += $3; count++ } END { print total/count }' file.txt

but how can I add for loop in it to have the average for every variable in column 1?

The file is tab-separated.

Thank you

Best Answer

Miller is also handy for tasks like this ex.

$ mlr --nidx stats1 -a mean -f 3 -g 1 file.txt
a 2.500000
b 9.666667

or (with a more recent version that has the format-values verb)

$ mlr --nidx stats1 -a mean -f 3 -g 1 then format-values -f '%.2f' file.txt
a 2.50
b 9.67
Related Question