views:

97

answers:

3

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!

+2  A: 

Try it with awk:

awk '{ count[$2] += $1 } END { for(elem in count) print count[elem], elem }' 1.out 2.out
Philipp
Ok, it should work for me. It's not ideal because I expect to do it with O(N) memory usage, where N is number of files but it will work for some time (unless I have big results). Thanks!
Radarek
I don't think it's linear in the number of files because `awk` reads all files in sequence, one line at a time, and it only needs to keep the `count` array (hash table?) in memory.
Philipp
I didn't say that solution given by Philipp is linear. I said that it can be written such a solution.
Radarek
A: 

It's quite a specific problem, so it's unlikely any tool will do this by default. You can script it in a small enough loop (no need for awk nastyness), implemented in any scripting language (even sh). I don't think there's another way.

wds
A: 

This is not quite serious (but it works). I like Philipps solution.

cat 1.out 2.out |
{
    while read line; do
        for i in $(seq ${line% *}); do
            echo ${line#* }
        done
    done
} | sort | uniq -c
andre-r