Linux uniq Command Tutorial With Examples

Linux have a lot of tools to make daily work of system administrators easy. uniq is one of them. Uniq is a small tool with limited capabilities.

Syntax

Syntax is same as other most of the Linux commands.

uniq [OPTION]... [INPUT [OUTPUT]]

Help

Simple and fast help about the options can be get like below.

$ uniq --help
Help
Help

Example File

While looking various options and features of the uniq command we need some sample text to use in the examples. Below there is file named a.txt file

a.txt

This is Line 1 
This IS 2 
And an other Line
This IS 2
This IS 3
What a other Line

Show Occurrence Count

This will show how many times each line occurred. This will create output by prefixing occurrence count with the lines.

$ uniq -c a.txt
Show Occurrence Count
Show Occurrence Count

Only Print Duplicate Lines

There is another useful options which will show duplicate lines in the text file. This option -d will only show duplicate lines without giving any count.

$ uniq -d a.txt
Only Print Duplicate Lines
Only Print Duplicate Lines

Skip Specified Field From Comparing

We can specify some field for skipping them comparing. This can be useful to set specific text to compare. As we have a text file where This column is the first field most of the lines. We can skip and start from second field. Columns are delimited with spaces or tabs.

$ uniq -f 1 -c  a.txt
Skip Specified Field From Comparing
Skip Specified Field From Comparing

Skip Specified Chars From Comparing

Some times it can be useful to skip some starting chars from comparison. The index of the starting character can be specified with -s option like below. In this example we will start comparing from 5th character.

$ uniq -s 5 -c  a.txt
Skip Specified Chars From Comparing
Skip Specified Chars From Comparing

Ignore Case

Sort command is by default case sensitive. Case sensitivity behaves differently for uppercase and lowercase characters. This default case sensitivity can be disabled with -i character like below.

$ uniq -i  a.txt

Only Print Unique Lines

One of the most popular usage is printing only unique lines. Only unique lines can be printed with -u options.

$ uniq -u a.txt

Version

Version can be printed with --version option.

$ uniq --version
Version
Version

Leave a Comment