Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge results from uniq -c

I have many files with results of command: uniq -c some_file > some_file.out

For example: 1.out:

 1 a
 2 b
 4 c

2.out

 2 b
 8 c

I would like to merge these results, so I get:

 1 a
 4 b
 12 c

I thought that sort or uniq could handle it but I don't see any option related to it. Writing some ruby/perl script is one of way to go but I'd like to do it easly with core *nix commands (like mentioned sort and uniq).

Edit: To be clear. I don't have original files and I have to merge *.out files.

Thanks for help!

like image 447
radarek Avatar asked Sep 25 '09 09:09

radarek


1 Answers

Try it with awk:

awk '{ count[$2] += $1 } END { for(elem in count) print count[elem], elem }' 1.out 2.out 
like image 69
Philipp Avatar answered Oct 06 '22 20:10

Philipp