I often use sort | uniq -c
to make count statistics.
Now, if I have two files with such count statistics, I would like to put them together and add the counts. (I know I could append the original files and count there, but lets assume only the count files are accessible).
For example given:
a.cnt:
1 a
2 c
b.cnt:
2 b
1 c
I would like to concatenate and get the following output:
1 a
2 b
3 c
What's the shortest way to do this in the shell?
Edit:
Thanks for the answers so far!
Some possible side-aspects one might want to consider additionally:
- what if a, b, c are arbritrary strings, containing arbitrary white-spaces?
- what if the files are too big to fit in memory? Is there some
sort | uniq -c
-style command line option for this case that only looks at two lines at a time?