问题
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