linux - Average of multiple files with unequal row sizes in Shell -
i have 15 datafiles unequal row sizes, number of columns in each file same. e.g.
ifile1.dat ifile2.dat ifile3.dat , on ............ 0 0 0 0 1 6 1 2 5 3 2 7 2 5 6 10 4 6 5 2 8 9 5 9 10 2 10 3 8 2
in each file 1st column represents index number. compute average of these files each index number in column 1. i.e.
ofile.txt 0 0 [this computed (0+0)/2] 1 4 [this computed (2+6)/2] 2 6 [this computed (5+7)/2] 3 [no value] 4 6 [this computed (6)/1] 5 4.66 [this computed (2+3+9)/3] 6 10 7 8 5.5 9 10 2.5
i can't think of simple method it. thinking of method, seems lengthy. taking average after converting files same row size, .e.g.
ifile1.dat ifile2.dat ifile3.dat , on ............ 0 0 0 0 0 0 1 2 1 1 6 2 5 2 2 7 3 3 3 4 4 4 6 5 2 5 3 5 9 6 6 10 6 7 7 7 8 8 9 8 2 9 9 9 10 2 10 3 10
$ awk '{s[$1]+=$2; c[$1]++;} end{for (i in s) print i,s[i]/c[i];}' ifile*.dat 0 0 1 4 2 6 4 6 5 4.66667 6 10 8 5.5 10 2.5
in above code, there 2 arrays, s
, c
. s[i]
sum of entries index i
, c[i]
number of entries index i
. after have read files, print average, s[i]/c[i]
, each index i
.
Comments
Post a Comment