AWK:取代他们的平均每n行,每一列 [英] Awk: substituting each n lines with their average, for each column
本文介绍了AWK:取代他们的平均每n行,每一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能用awk替换文件的每个n线,他们的平均?
这问题答案完全,除了它只能处理一列和 3
不是参数化的事实:
<一href=\"http://stackoverflow.com/questions/8551349/how-to-sum-up-every-10-lines-and-calculate-average-using-awk\">How总结起来每10行,用AWK计算平均?
基本上我想借这样一个文件
1
2 1
3 1
4 1
5 1
6 1
2.5 2.0
3.5 2.0
4 2.0
和获得是这样的:
2 1
5 1
3.33 2.0
解决方案
下面是一个完整的shell脚本:
的awk -v计数= 3'
{
如果(NF&GT; tot_col)
tot_col = NF; CUR = 1;
而(CUR&LT; = NF)
{
总和[CUR] + = $ CUR;
CUR ++;
} 如果((NR%计)== 0)
{
CUR = 1;
而(CUR&LT; = tot_col)
{
的printf(%0.2F,数额[CUR] /计数);
CUR ++;
} 打印; 删除款项;
tot_col = 0;
}
}'$ @
How can I substitute each n lines of a file with their average using awk?
This question answer perfectly, except for the fact that it handles only one column and the 3
is not parametrized:
How to sum up every 10 lines and calculate average using AWK?
basically I would like to take a file like this
1 1
2 1
3 1
4 1
5 1
6 1
2.5 2.0
3.5 2.0
4 2.0
and obtain something like this:
2 1
5 1
3.33 2.0
解决方案
Here's a complete shell script:
awk -v count=3 '
{
if ( NF > tot_col )
tot_col = NF;
cur = 1;
while ( cur <= NF )
{
sums[cur] += $cur;
cur++;
}
if ( ( NR % count ) == 0 )
{
cur = 1;
while ( cur <= tot_col )
{
printf("%0.2f ", sums[cur] / count);
cur++;
}
print "";
delete sums;
tot_col = 0;
}
}' "$@"
这篇关于AWK:取代他们的平均每n行,每一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文