The Linux Newbie Guide  ⇒    Fundamentals     Advanced     Supplement   Command Index   ENG⇒中
All rights reserved, please indicate the source when citing
  

uniq - Remove Adjacent Duplicate Lines



The uniq command is used to remove adjacent duplicate lines from a file. It will only remove lines that are duplicates of the immediately preceding line. If there are duplicates that are not adjacent to each other, they will not be removed.

The basic syntax for the uniq command is as follows:
uniq [OPTIONS] [IN_FILE] [OUT_FILE] .

Here are some common options for the uniq command:


For example:
$ echo -e ' aaa\n aaa\n bbb\n aaa\n' | uniq ← 用〝uniq〞 刪除相鄰且重複的行
aaa
aaa ← which is adjacent to the previous row and duplicates will be deleted
bbb
aaa ←this line is not deleted

n the above test, the line "aaa" appeared three times, but it was only removed when it was adjacent to the previous line. The non-adjacent duplicate line was not removed.

For more uniq options and examples, you can refer to the provided link.