The uniq command in Linux

Working with the Linux terminal implies familiarity with manipulating text files. In this sense, the uniq command is a great help to analyze it. So in this post, you will learn how to use it and thus have another working tool.

The uniq command in Linux

This command allows you to filter the single lines of any text file. For this command to work perfectly, the files must be sorted, since it works with adjacent lines.

Therefore, the uniq command removes the lines that are repeated, leaving the output with lines that are not repeated. This is vital for purchasing files or for determining duplicate configurations in some of them.

uniq belongs to the GNU Core Utils, so we can have it available in all Linux distributions currently available. Like other commands, we will not have to install or configure anything to use it.

Let’s go for it.

Using the uniq command in Linux

The basic syntax of the uniq command is as follows

uniq [option]... [input [output]]]

So let’s explain with examples how to use the uniq command.

Let’s start from an example file called example.txt which has the following content

cat example.txt
Hello
Hello
Welcome to the blog
Welcome to the blog
Enjoy
Enjoy

If we use the uniq command without any option, then the repeated lines of the file will be omitted.

uniq example.txt

Output:

Hello
Welcome to the blog
Enjoy

If you want to know the number of times the line is repeated, then you can use the -c option.

uniq -c example.txt

Output:

2 Hello
2 Welcome to the blog
2 Enjoy

Quite useful, this option, and one of my favorites.

If you just want to get the duplicate lines from the file, then the -d option is the one you need.

uniq -d example.txt

When comparing text files, upper and lower case is always something to keep in mind. To exclude them from the uniq command parsing, then you have to use the -i option.

uniq -i example.txt

It is as simple as that.

In addition to this, you can print only the unique lines in the file with the -u option.

uniq -u example.txt

It is so simple to use, but in daily work it can be very useful.

Conclusion

In this post, you have learned how to use the uniq command in Linux. This simple to use and learn command allows us to have an alternative for text file comparison.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top