3 ms·
awk: echo '1 "one"\n1 "two"\n1 "three"\n1 "one"\n1 "four"\n1 "two"' | awk '{ counters[$2] += $1 } END { for (id in counters) { print counters[id], id } }'
by asa400 3y ago
awk:
echo '1 "one"\n1 "two"\n1 "three"\n1 "one"\n1 "four"\n1 "two"' | awk '{ counters[$2] += $1 } END { for (id in counters) { print counters[id], id } }'
2 "two"
1 "four"
1 "three"
2 "one"
- mjcohen 3y agoAnd you can use the system sort or gawk's internal sort to get them in order.