Shell中具有不相等行大小的多个文件的平均值

时间:2015-08-03 06:09:45

标签: linux shell unix awk average

我有15个不等行大小的数据文件,但每个文件中的列数相同。例如

ifile1.dat   ifile2.dat  ifile3.dat and so 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

在每个文件中,第1列表示索引号。 我想计算第1列中每个索引号的所有这些文件的平均值。即

ofile.txt
 0   0     [This is computed as (0+0)/2]
 1   4     [This is computed as (2+6)/2]
 2   6     [This is computed as (5+7)/2]
 3         [no value]
 4   6     [This is computed as (6)/1]
 5   4.66  [This is computed as (2+3+9)/3]
 6   10
 7   
 8   5.5
 9   
 10  2.5

我想不出任何简单的方法来做到这一点。我在考虑一种方法,但似乎非常冗长。转换所有具有相同行大小的文件后取平均值,例如。

ifile1.dat   ifile2.dat  ifile3.dat and so 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

1 个答案:

答案 0 :(得分:3)

$ 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

在上面的代码中,有两个数组scs[i]是索引为i的所有条目的总和,c[i]是索引为i的条目数。在我们阅读完所有文件后,我们会为每个索引s[i]/c[i]打印平均值i