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


this question related earlier post find maximum values in 2nd column each distinct values in 1st column using linux

i have 2 columns 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 find average values in 2nd column 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 it, though know average command.

using awk 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 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -