Description and use of uniq command in Linux operating system

Time:2022-6-15

The function of uniq command: display unique lines, and only display those lines that repeat consecutively once!

Next, through practical examples:

[[email protected] ~]# cat test
boy took bat home
boy took bat home
girl took bat home
dog brought hat home
dog brought hat home
dog brought hat home

If you look at the contents of the test file, you can see the continuous repeated lines

[[email protected] ~]# uniq test
boy took bat home
girl took bat home
dog brought hat home

The uniq command does not add any parameters, and only displays the continuously repeated lines once

[[email protected] ~]# uniq -c test
2 boy took bat home
1 girl took bat home
3 dog brought hat home

-The C parameter displays the number of consecutive occurrences of each line in the file.

[[email protected] ~]# uniq -d test
boy took bat home
dog brought hat home

-The D option displays only the rows that appear consecutively and repeatedly in the file.

[[email protected] ~]# uniq -u test
girl took bat home

-The U option displays lines that do not appear consecutively in the file.

[[email protected] ~]# uniq -f 2 -s 2 test
boy took bat home

Ignore the first 2 fields of each line, ignore the second blank character and the first character of the third field, and the result is at home

[[email protected] ~]# uniq -f 1 test
boy took bat home
dog brought hat home

Ignore the first field of each line, so that the lines beginning with boy and girl appear to be consecutive and repeated lines.