How to do uniq -d without presorting (or something similar)

…衆ロ難τιáo~ 提交于 2021-01-29 03:54:13

问题


I am aware that I can remove duplicated lines without presorting, for example:

awk '!x[$0]++' file

However, my goal is to only print lines which are duplicated and only once. If it were not for the presorting problem

sort | uniq -d

would be perfect. BUT the order is of great importance to me. Is there a way to do this with awk, grep or something similar?

I am looking for a one liner which does not require writing a script if possible.


回答1:


Just check the value of x[$0]:

awk 'x[$0]++ == 1' file.txt

The above will print a line when it's seen for the second time.

Or with prefixed ++:

awk '++x[$0] == 2' file.txt



回答2:


Try simply removing ! from your awk command.

bash-4.1$ cat /tmp/inp.txt 
abc
def
abc
xyz
def
hello

bash-4.1$ awk 'x[$0]++ == 1' /tmp/inp.txt 
abc
def

Hope this is what you expect!!



来源:https://stackoverflow.com/questions/38203260/how-to-do-uniq-d-without-presorting-or-something-similar

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