1

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.

Community
  • 1
  • 1
Kay
  • 1,957
  • 2
  • 24
  • 46

1 Answers1

2

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
anubhava
  • 761,203
  • 64
  • 569
  • 643