0

I have 2 columns in a text file with only one space between them. How can I join the duplicate 2 letters and sum their number together with awk

2 xx
4 dd
1 xx
8 mm 
5 dd
1 xx

So I can have this outcome:

4 xx
9 dd
8 mm

migrated from serverfault.com May 28 '18 at 10:15

This question came from our site for system and network administrators.

3
awk '{S[$2]+=$1}; END {for (T in S) {print S[T],T}}' << -OEF-
2 xx
4 dd
1 xx
8 mm
5 dd
1 xx
-OEF-

gives

9 dd
4 xx
8 mm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.