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 is same as other most of the Linux commands.
uniq [OPTION]... [INPUT [OUTPUT]]
Simple and fast help about the options can be get like below.
$ uniq --help
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
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
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
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 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
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
$ uniq -u a.txt
Version can be printed with
$ uniq --version