这听起来像是一项工作awk
:) 将程序的输出通过管道传输到以下awk
脚本:
your_program | awk '{a[$1]+=$2}END{for(name in a)print name " " a[name]}'
输出:
Sean 201
Bob 219
Jim 245
Mark 190
Richard 142
John 208
awk
脚本本身可以用这种格式更好地解释:
# executed on each line
{
# 'a' is an array. It will be initialized
# as an empty array by awk on it's first usage
# '$1' contains the first column - the name
# '$2' contains the second column - the amount
#
# on every line the total score of 'name'
# will be incremented by 'amount'
a[$1]+=$2
}
# executed at the end of input
END{
# print every name and its score
for(name in a)print name " " a[name]
}
请注意,要获得按分数排序的输出,您可以将另一个管道添加到sort -r -k2
. -r -k2
以相反的顺序按第二列排序:
your_program | awk '{a[$1]+=$2}END{for(n in a)print n" "a[n]}' | sort -r -k2
输出:
Jim 245
Bob 219
John 208
Sean 201
Mark 190
Richard 142