shell: any way to use `uniq -c` taking into account a prior `uniq -c`?

StackOverflow https://stackoverflow.com/questions/19674137

  •  01-07-2022
  •  | 
  •  

سؤال

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?

هل كانت مفيدة؟

المحلول

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]); }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top