Strange behavior of uniq on darwin shells
Asked Answered
A

2

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.

Aorist answered 18/4, 2011 at 6:36 Comment(0)
R
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.

Remuneration answered 18/4, 2011 at 6:39 Comment(4)
developer.apple.com/library/mac/#documentation/Darwin/Reference/…Hamish
@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.Remuneration
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.Preform
@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.Remuneration
T
1

You can try this awk solution

awk '{a[$0]++}END{for(i in a)if(a[i]>1){ print i ,a[i] } }' file
Tica answered 18/4, 2011 at 6:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.