【问题标题】:Average of multiple files with unequal row sizes in ShellShell中具有不相等行大小的多个文件的平均值
【发布时间】:2015-08-03 06:09:44
【问题描述】:

我有 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 列中每个索引号的所有这些文件的平均值。即

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

【问题讨论】:

    标签: linux shell unix awk average


    【解决方案1】:
    $ 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 的条目数。读取所有文件后,我们打印每个索引i 的平均值s[i]/c[i]

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    相关资源
    最近更新 更多