Let's say I have a text file with lines like these:
foo 10
bar 15
bar 5
foo 30
...
What's the simplest way to generate the following output:
foo 40
bar 20
?
This will do:
awk '{arr[$1]+=$2;} END { for (i in arr) print i, arr[i]}' file
For more information, read on Awk's associative arrays.
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