Is there way to delete duplicate lines in a file in Unix?
I can to it with sort -u
and uniq
commands. but I want to use sed
or awk
.
Is that possible?
Is there way to delete duplicate lines in a file in Unix?
I can to it with sort -u
and uniq
commands. but I want to use sed
or awk
.
Is that possible?
From http://sed.sourceforge.net/sed1line.txt: (Please don't ask me how this works ;-) )
# delete duplicate, consecutive lines from a file (emulates "uniq").
# First line in a set of duplicate lines is kept, rest are deleted.
sed '$!N; /^\(.*\)\n\1$/!P; D'
# delete duplicate, nonconsecutive lines from a file. Beware not to
# overflow the buffer size of the hold space, or else use GNU sed.
sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'
sed
is a stream editor and doesn't naturally treat input files as lines
It may be possible, but I think other techniques will be easier.
EDIT: well, Andre Miller's answer shows it's not, erm, intuitive