如何用linux总结数字

时间:2013-05-15 22:54:58

标签: linux bash

我做了cat results.txt | grep eval而我得到了

eval: -2.72907
baseline eval (random): -0.031584202184
eval: 0.807805
baseline eval (random): 0.0227601966463
eval: 2.0625
baseline eval (random): 0.0138953249621

如何使用命令行中的linux命令分别对evalbaseline eval进行求和?

1 个答案:

答案 0 :(得分:6)

Awk可以做到

% grep eval results.txt | awk -F: '{a[$1]+=$2}END{for(i in a)print i ": " a[i]}'
eval: 0.141235
baseline eval (random): 0.00507132

更好的是,正如Johnsyweb在评论中提到的那样,让也进行搜索:

awk -F: '/eval/{a[$1]+=$2}END{for(i in a)print i ": " a[i]}' results.txt