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]]
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.
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.
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.
Similar Readings
- The “grep” Command in Linux [10+ Practical Examples]
- The “wc” Command in Linux [15 Practical Examples]
- The “sort” Command in Linux [16 Practical Examples]
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.
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.
Similar Readings
- The “nano” Command in Linux [13 Practical Examples]
- The “cut” Command in Linux [8 Practical Examples]
- The “jed” Command in Linux [8 Practical Examples]
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.
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.
Similar Readings
- The “vi” Command in Linux [6 Practical Examples]
- The “vim” Command in Linux [8 Practical Examples]
- The “egrep” Command in Linux [10+ Practical Examples]
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.
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
- The “paste” Command in Linux [6 Practical Examples]
- The “split” Command in Linux [6 Practical Examples]
- The “sed” Command in Linux [7 Practical Examples]
- The “tr” Command in Linux [6 Practical Examples]
- The “printf” Command in Linux [8 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners