我有 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
最佳答案
$ 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
在上面的代码中,有两个数组,s
和c
。 s[i]
是索引为i
的所有条目的总和,c[i]
是索引为i 的条目数
。读取所有文件后,我们打印每个索引 i
的平均值 s[i]/c[i]
。
关于linux - Shell 中行大小不等的多个文件的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31780447/