The “uniq” Command in Linux [6 Practical Examples]

The uniq command in Linux is a handy tool to report or filter out repeated lines in a text file. In short, the uniq command in Linux simply detects a match of any adjacent line inside a text file and prints the number of duplicate lines, the duplicate lines, the unique lines, etc.

A. Description

The uniq command helps to detect any duplicate entity inside a text file to avoid redundancy.

B. Syntax

The uniq command in Linux takes OPTION, INPUT, and OUTPUT. The syntax for the uniq command is given below.

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

Note: Here, the OPTION, INPUT, and OUTPUT enclosed by square brackets mean that OPTION, INPUT, and OUTPUT are not mandatory for the command. Furthermore, three dots preceded by OPTION means that the uniq command can take multiple OPTIONs.

C. Options

Different options can be added to the syntax of the uniq command to modify the command. Here, I have listed some valuable options below. Nevertheless, If you do not find your desired option here, you can look for it on the man (manual) page. To go to the man page, type the following command and press ENTER.

man uniq

Useful Options

  • -c/–count: It sets a prefix at the start of the lines by the number of occurrences.
  • -d/–repeated: It only prints the duplicate lines, one for each group.
  • -D: It prints all the duplicate lines.
  • -f/–skip-fields=N: It avoids comparing the first N fields.
  • –help: It displays the help section and exit.
  • -i/–ignore-case: It ignores the differences in the case when comparing.
  • -u/–unique: It only prints the unique lines.
  • –version: It prints the version information and exits.
Note: All options in Linux are case-sensitive. Therefore, you must be careful while using these.

Practical Examples of the “uniq” Command in Linux

You can easily filter out the repeated line from a text file using the uniq command. Some practical examples of the uniq command in Linux are shown below. Here, I will work with the following mv.txt and mv2.txt files. I will work with the mv.txt and mv2.txt files.

Example 1: Printing the Number of Times a Line is Repeated

You can print the number of times a line is repeated using the -c option with the uniq command in Linux. Here, I will print the number of lines repeated in the file mv.txt. To do so, follow the below procedures.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Then, type the following command in the command prompt.

uniq -c mv.txt

➌ Now, press the ENTER button.

Output >

The following image depicts that the -c option with the uniq command has printed the number of times a line is repeated.The -c option with the uniq command has printed the number of times a line is repeated.


Similar Readings


Example 2: Printing the Repeated Lines for One Time Using the “uniq” Command in Linux

You can print only the repeated lines using the -d option with the uniq command in Linux. Here, I will print only the repeated lines of the mv.txt file. To achieve so, follow the given steps.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Then, copy the following command in the terminal.

uniq -d mv.txt

➌ Now, tap the ENTER button.

Output >

The -d option of the uniq command has printed only the repeated line from the mv.txt file, as illustrated in the following image.The -d option of the uniq command has printed only the repeated line from the mv.txt file.

Example 3: Printing All the Duplicate Lines Using the “uniq” Command in Linux

You can print all the duplicate lines using the -D option with the uniq command in Linux. Here, I will print all the duplicate lines of the mv.txt file. To achieve so, follow the given steps.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Then, type the following command in the command prompt.

uniq -D mv.txt

➌ Now, press the ENTER button.

Output >

The following image illustrates that I have printed all the duplicate lines using the -D option with the uniq command.I have printed all the duplicate lines using the -D option with the uniq command.


Similar Readings


Example 4: Printing Only the Unique Lines Using the “uniq” Command in Linux

You can print only the unique lines using the -u option with the uniq command in Linux. Here, I will print only the unique lines of the mv.txt file. To do so, follow the given steps.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Then, type the following command in the command prompt.

uniq -u mv.txt

➌ Now, press the ENTER button.

Output >

The -u option of the uniq command has printed only the unique line from the mv.txt file, as depicted in the following image.The -u option of the uniq command has printed only the unique line from the mv.txt file.

Example 5: Case-insensitive Comparison Using the “uniq” Command in Linux

You can make a case-insensitive comparison using the -i option with the uniq command in Linux. Here, I will print only the unique line making the case-insensitive comparison of the mv2.txt file. To do so, follow the given steps.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Then type the following command in the command prompt.

uniq -i mv2.txt

➌ Now, press the ENTER button.

Output >

The below image shows that the -i option with the uniq command has only printed the unique lines of the mv2.txt file, making the case-insensitive comparison.The -i option with the uniq command has only printed the unique lines of the mv2.txt file, making the case-insensitive comparison.


Similar Readings


Example 6: Display the Version of the “uniq” Command in Linux

You can print the version information of the uniq command currently running in your system. Here, I will display the version information of the uniq command currently running in my system. To achieve so, follow the procedures given below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Then, copy the following command in the command prompt:

uniq --version

➌ Finally, press the ENTER button.

Output >

The following image shows that I have displayed the version information of the uniq command on the terminal.I have displayed the version information of the uniq command on the terminal.

Conclusion

In this article, I have demonstrated the operation of finding any duplicate line inside a text file using the uniq command in Linux. Therefore, I hope you’ll be competent enough to explore more things with the help of these practical examples.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment