Question

I'm doing some data processing in a UNIX shell (OS X).

After using uniq -c and doing some further look-up based processing, I sort the data all over again, and need to use uniq -c one more time, but keep in mind the results of the prior counts (e.g. count the counts).

E.g. if the input data is something like this:

36351 | 3
36351 | 2
36351 | 13
2914 | 1
2914 | 2

Then the output should be something like this:

36351 | 18
2914 | 3

(The order of the fields is not substantial.)

How do I do this in shell?

Was it helpful?

Solution

I don't know of a way to get uniq to do the job, but it's pretty trivial with awk:

    { c[$1] += $3; } 

END { for (s in c) printf("%s | %s\n", s, c[s]); }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top