How to delete duplicate lines in a file without sorting it in Unix?

后端 未结 9 1610
Happy的楠姐
Happy的楠姐 2020-11-22 17:26

Is there a way to delete duplicate lines in a file in Unix?

I can do it with sort -u and uniq commands, but I want to use sed

9条回答
  •  一整个雨季
    2020-11-22 17:45

    The one-liner that Andre Miller posted above works except for recent versions of sed when the input file ends with a blank line and no chars. On my Mac my CPU just spins.

    Infinite loop if last line is blank and has no chars:

    sed '$!N; /^\(.*\)\n\1$/!P; D'

    Doesn't hang, but you lose the last line

    sed '$d;N; /^\(.*\)\n\1$/!P; D'

    The explanation is at the very end of the sed FAQ:

    The GNU sed maintainer felt that despite the portability problems
    this would cause, changing the N command to print (rather than
    delete) the pattern space was more consistent with one's intuitions
    about how a command to "append the Next line" ought to behave.
    Another fact favoring the change was that "{N;command;}" will
    delete the last line if the file has an odd number of lines, but
    print the last line if the file has an even number of lines.

    To convert scripts which used the former behavior of N (deleting
    the pattern space upon reaching the EOF) to scripts compatible with
    all versions of sed, change a lone "N;" to "$d;N;".

提交回复
热议问题