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.

uniq Command Syntax

Syntax is same as other most of the Linux commands.

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

uniq Command Help

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

$ uniq --help
Help
uniq Command Help

Example File

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

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 option 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 fields for skipping them comparing. This can be useful to set the 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 in the 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 the 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 sensitivity For uniq Command

The 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 usages is printing only unique lines. Only unique lines can be printed with -u options.

$ uniq -u a.txt

uniq Command Version

Version can be printed with --version option.

$ uniq --version
Version
Version

LEARN MORE  Linux History Command Tutorial with Examples

Leave a Comment