对键排序并以bash求和 [英] Sort keys and Sum their values in bash
本文介绍了对键排序并以bash求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串列表(stdin),如下所示:
I have a list of strings (stdin) like next:
1 pineapples
28 apples
16 oranges
8 apples
2 apples
2 oranges
56 pineapples
是否存在一种本机方式(例如sort
和uniq -c
),我可以通过这种方式合并和求和:
Is there a native way (like sort
& uniq -c
) with which I can merge and sum them like this:
38 apples
18 oranges
57 pineapples
像sort |uniq -c
一样,但不仅限于出现次数?
like sort |uniq -c
do, but not only for occurrences number?
推荐答案
尝试以下方法:
awk '{a[$2] += $1} END{for (i in a) print a[i], i}' < in.txt
输出
38 apples
57 pineapples
18 oranges
这篇关于对键排序并以bash求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文