Delete Line If Next Line Is the Same – Text Processing Guide

awksedtext processing

What sed/awk command can I use? Just sort -u will remove all instances

Input:

abc
abc
def
abc
abc
def

Expected output:

abc
def
abc
def

Best Answer

That's what the uniq standard command is for.

uniq your-file

Note that some uniq implementations like GNU uniq will give you the first of a sequence of lines that sort the same (where strcoll() returns 0) as opposed to are byte-to-byte identical (where memcmp() or strcmp() returns 0). To force a byte to byte comparison regardless of the uniq implementation, you can force the locale to C with:

LC_ALL=C uniq your-file
Related Question