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
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.