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.


Source: shell

Leave a Reply