Strange behavior of uniq on darwin shells

ⅰ亾dé卋堺 提交于 2019-12-10 17:25:19

问题


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.


回答1:


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.




回答2:


You can try this awk solution

awk '{a[$0]++}END{for(i in a)if(a[i]>1){ print i ,a[i] } }' file


来源:https://stackoverflow.com/questions/5699339/strange-behavior-of-uniq-on-darwin-shells

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!