结束{for(ke in sum)打印键“:” sum [sum [key]/count [key]}> :什么时候文件已经完成解析,我们打印每个组以及此组的sum/Count
It's fairly simple to do this in awk :
awk '{sum[$1]+=$2; count[$1]++} END {for(key in sum) print key ": " sum[key]/count[key]}' input_file
Output for your sample file :
grp1: 2
grp2: 6.5
grp4: 9
Explanation :
{sum[$1]+=$2; count[$1]++} : for every line of your input file, we use 2 associative arrays
count that stores the number of time the 1st field is encountered
sum that stores the sum of every value for the 2nd field for this specific group
END {for(key in sum) print key ": " sum[key]/count[key]} : when your file has finished parsing, we print every group, as well as the sum/count for this group
发布评论
评论(2)
在
awk
中进行此操作非常简单:示例文件的输出:
{sum [sum [$ 1]+= $ 2;计数[$ 1] ++}
:对于输入文件的每一行,我们使用2个关联数组计数
存储遇到第一字段的时间sum
存储此特定组的第二个字段的每个值的总和结束{for(ke in sum)打印键“:” sum [sum [key]/count [key]}
> :什么时候文件已经完成解析,我们打印每个组以及此组的sum/Count
It's fairly simple to do this in
awk
:Output for your sample file :
Explanation :
{sum[$1]+=$2; count[$1]++}
: for every line of your input file, we use 2 associative arrayscount
that stores the number of time the 1st field is encounteredsum
that stores the sum of every value for the 2nd field for this specific groupEND {for(key in sum) print key ": " sum[key]/count[key]}
: when your file has finished parsing, we print every group, as well as thesum/count
for this group给定:
打印:
如果您希望它们全部都是浮点呈现:
打印:
知道awk中的关联阵列不维护顺序,因此
grpx
可能会从文件中的顺序变化。Given:
Prints:
Of if you want them all to be floating point presentation:
Prints:
Know that associative arrays in awk do not maintain order so the
grpX
may change from the order found in the file.