Kay
Kay

Reputation: 2067

Find the average values in 2nd column for each distinct values in 1st column using Linux

This is a question related to my earlier post Find the maximum values in 2nd column for each distinct values in 1st column using Linux

I have two columns as follows

ifile.dat
1   10
3   34
1   4
3   32
5   3
2   2
4   20
3   13
4   50
1   40
2   20

What I look for is to find the average values in 2nd column for each 1,2,3,4,5 in 1st column.

ofile.dat
1   18    i.e. (10+4+40)/3 
2   11    i.e. (2+20)/2
3   26.33 i.e. (34+32+13)/3
4   35
5   3

I can't able to do it, though I know the average command.

Upvotes: 1

Views: 86

Answers (1)

anubhava
anubhava

Reputation: 785156

Using awk you can do:

awk '{a[$1]+=$2; c[$1]++} END{for (i in a) printf "%d%s%.2f\n", i, OFS, a[i]/c[i]}' file
1 18.00
2 11.00
3 26.33
4 35.00
5 3.00

Upvotes: 2

Related Questions