I have a list of strings (stdin) like next:
1 pineapples
28 apples
16 oranges
8 apples
2 apples
2 oranges
56 pineapples
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
like sort |uniq -c
do, but not only for occurrences number?
Try this one:
awk '{a[$2] += $1} END{for (i in a) print a[i], i}' < in.txt
The output
38 apples
57 pineapples
18 oranges
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With