5

I've used 'uniq -d -c file' in many shell scripts on linux machines, and it works. On my MAC (OS X 10.6.7 with developer tools installed) it doesn't seems to work:

$ uniq -d -c testfile.txt 
usage: uniq [-c | -d | -u] [-i] [-f fields] [-s chars] [input [output]]

It would be nice if anyone could checks this.

chmoelders
  • 18,584
  • 6
  • 22
  • 24

2 Answers2

6

Well, it's right there in the Usage message. [ -c | -d | -u] means you can use one of those possibilities, not two.

Since OSX is based on BSD, you can check that here or, thanks to Ignacio, the more Apple-specific one here.

If you want to achieve a similar output, you could use:

do_your_thing | uniq -c | grep -v '^ *1 '

which will strip out all those coalesced lines that have a count of one.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/uniq.1.html – Ignacio Vazquez-Abrams Apr 18 '11 at 06:40
  • @former, that _is_ annoying. I suspect that the software is collapsing under the weight of philosophy when trying to figure out how to both output only duplicated lines as well as only unique lines :-) Sounds very much like a bug based on the usage. – paxdiablo Apr 18 '11 at 06:50
  • I disagree with the statement "-d ... doesn't make much sense when you're coalescing them with a count". I believe your reasoning is that because each duplicate line will be output only once, the count for each line will end up being 1. This is an incorrect interpretation. The OSX manpage states that the flag "-c" shall "precede each output line with the count of the number of times the line occurred in the input, followed by a single space." The count depends on the input, not the output. Unfortunately it seems that Apple has sided with this incorrect interpretation. – some guy Jul 16 '14 at 23:33
  • @someguy, since it's Apple's software (BSD's really but their man page states the same thing as Apple's), I think you'll find theirs is the _correct_ interpretation :-) Anyhow, the usage message is clear: it allows _one_ option from the group `{-c,-d,-u}` (aforementioned bug allowing both d and u notwithstanding). Since my supposition wasn't really a central part of my answer, I'll get rid of it. – paxdiablo Jul 17 '14 at 01:30
1

You can try this awk solution

awk '{a[$0]++}END{for(i in a)if(a[i]>1){ print i ,a[i] } }' file
kurumi
  • 25,121
  • 5
  • 44
  • 52