找到字段中的列的平均 [英] Find the average of fields in the columns
本文介绍了找到字段中的列的平均的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的记录
1 11 11 T A 0 0 A = 1,B = 2,C = 3; D = 4;
有8列,每列由标签空格隔开
和我需要的输出是这样的。
1 11 11 T A 0 0 A = 1,B = 2,C = 3; D = 4; AF = 2.5;
其中, AF
是在8列所有字段的平均值。
AF =(1 + 2 + 3 + 4)/4=2.5
解决方案
在intput数据是非常好的:空格,逗号,分号......试试这个:
的awk -F[;,]''{N = 0; S = 0;对于(i = 1; I< = NF;我++)
如果($ I〜/ = [0-9] + /){分($ I,T,=); N +; S + = T [2]; }
打印$ 0AF =S / N;
}'文件
与您的数据:
肯特$回声1 11 11 TA 0 0 A = 1,B = 2,C = 3; D = 4;| awk的-F'[;,]'' {N = 0; S = 0;对于(i = 1; I< = NF;我++)
如果($ I〜/ = [0-9] + /){分($ I,T,=); N +; S + = T [2]; }
打印$ 0AF =S / N;
}
1 11 11 T A 0 0 A = 1,B = 2,C = 3; D = 4; AF = 2.5;
I have a record like this
1 11 11 T A 0 0 A=1;B=2,C=3;D=4;
there are 8 columns and each columns is separated by tab space
and I need the output is like this
1 11 11 T A 0 0 A=1;B=2,C=3;D=4;AF=2.5;
Where AF
is the average of all fields in the 8th column.
AF=(1+2+3+4)/4=2.5
解决方案
you intput data is really nice: space, comma, semicolon... try this:
awk -F'[;, ]' '{n=0;s=0;for(i=1;i<=NF;i++)
if($i~/=[0-9]+/){split($i,t,"=");n++;s+=t[2]; }
print $0"AF="s/n";"
}' file
with your data:
kent$ echo "1 11 11 T A 0 0 A=1;B=2,C=3;D=4;"|awk -F'[;, ]' '{n=0;s=0;for(i=1;i<=NF;i++)
if($i~/=[0-9]+/){split($i,t,"=");n++;s+=t[2]; }
print $0"AF="s/n";"
}'
1 11 11 T A 0 0 A=1;B=2,C=3;D=4;AF=2.5;
这篇关于找到字段中的列的平均的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文