我有一个看起来像这样的文件:
[root@localhost ~]# cat output.txt
0.0 709312 gnome-session-b dan
0.7 3662292 \_ gnome-shell dan
0.0 1157420 \_ gnome-softw dan
0.0 903172 gnome-shell-cal dan
0.0 286580 gnome-keyring-d dan
0.0 709312 gnome-session-b dan
0.7 3662292 \_ gnome-shell dan
0.0 1157420 \_ gnome-softw dan
0.0 903172 gnome-shell-cal dan
0.0 286580 gnome-keyring-d dan
0.0 709312 gnome-session-b dan
0.7 3662292 \_ gnome-shell dan
0.0 1157420 \_ gnome-softw dan
0.0 903172 gnome-shell-cal dan
0.0 286580 gnome-keyring-d dan
0.0 709312 gnome-session-b dan
0.7 3662292 \_ gnome-shell dan
0.0 1157420 \_ gnome-softw dan
0.0 903172 gnome-shell-cal dan
0.0 286580 gnome-keyring-d dan
0.0 709312 gnome-session-b dan
0.7 3662292 \_ gnome-shell dan
0.0 1157420 \_ gnome-softw dan
0.0 903172 gnome-shell-cal dan
0.0 286580 gnome-keyring-d dan
0.0 709312 gnome-session-b dan
0.7 3662292 \_ gnome-shell dan
0.0 1157420 \_ gnome-softw dan
0.0 903172 gnome-shell-cal dan
0.0 286580 gnome-keyring-d dan
我如何对此进行排序并计算前两列的平均值?我有一个for循环,运行6次来填充这些数据,所以它只需要计算每6行的平均值。
答案 0 :(得分:2)
Bash是错误的工具 - 它不支持浮点数学。请改用awk
:
awk '
# Initialization: Run once at startup
BEGIN {
i=0;
sum1=0;
sum2=0;
}
# Run once per line
{
sum1+=$1;
sum2+=$2;
if(++i >= 6) {
print (sum1 / i) " " (sum2 / i);
sum1=0;
sum2=0;
i=0;
}
}
# Run once at the very end, for if our total number of lines was not divisible by 6
END {
if(i > 0) {
print (sum1 / i) " " (sum2 / i)
}
}
'