The Wayback Machine - https://web.archive.org/web/20240808052538/https://www.geeksforgeeks.org/uniq-command-in-linux-with-examples/
Open In App

uniq Command in Linux with Examples

Last Updated : 18 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

The uniq command in Linux is a command-line utility that reports or filters out the repeated lines in a file. In simple words, uniq is the tool that helps to detect the adjacent duplicate lines and also deletes the duplicate lines. uniq filters out the adjacent matching lines from the input file(that is required as an argument) and writes the filtered data to the output file. 

Syntax of uniq Command

The basic syntax of the `uniq` command is:

uniq [OPTIONS] [INPUT_FILE [OUTPUT_FILE]]

Here,

  • `OPTIONS`: Optional flags that modify the behavior of the `uniq` command.
  • `INPUT_FILE`: The path to the input file containing the text data. If not specified, `uniq` reads from the standard input (usually the keyboard).
  • `OUTPUT_FILE`: The path to the output file where the unique lines will be written. If not specified, `uniq` writes to the standard output (usually the terminal). 

Common Options of the uniq Command

Here are some common options that can be used with the `uniq` command:

Options

Description

-c, –count

Prefix lines by the number of occurrences in the input, followed by a space.

-d, –repeated

Only output lines that are repeated in the input.

-i, –ignore-case

Ignore differences in case when comparing lines.

-f, –skip-fields=N

Avoid comparing the first N fields in each line.

-s, –skip-chars=N

Avoid comparing the first N characters in each line.

-u, –unique

Only output lines that are unique in the input.

Examples of Using the `uniq` Command

Now, let’s understand the use of this with the help of an example. Suppose you have a text file named kt.txt which contains repeated lines that needs to be omitted. This can simply be done with uniq. 

//displaying contents of kt.txt//

$cat kt.txt
I love music.
I love music.
I love music.

I love music of Kartik.
I love music of Kartik.

Thanks.


How to Remove Duplicate Lines Using `uniq` Command

To remove duplicate lines from `kt.txt`, we can use the `uniq` command:

uniq kt.txt

remove duplicate lines

remove duplicate lines

As you can see that we just used the name of the input file in the above uniq example and as we didn’t use any output file to store the produced output, the uniq command displayed the filtered output on the standard output with all the duplicate lines removed. 

Note: uniq isn’t able to detect the duplicate lines unless they are adjacent to each other. The content in the file must be therefore sorted before using uniq or you can simply use sort -u instead of uniq command.  

How to Count Duplicate Lines Using `uniq` Command in Linux

The `-c` option prefixes each line with the number of occurrences in the input:

uniq -c kt.txt

Counting occurrence of duplicate lines

Counting occurrence of duplicate lines

In this example at the starting of each line its repeated number is displayed

How to Display Repeated Lines Using `uniq` Command in Linux

The `-d` option only prints duplicate lines:

uniq -d kt.txt

display only duplicate lines

display only duplicate lines

How to Display all Duplicate Lines Using `uniq` Command in Linux

The `-D` option prints all duplicate lines, not just one per group:

>
uniq -D kt.txt

prints all duplicate lines

prints all duplicate lines

How to Display Unique Lines Using `uniq` Command in Linux

The `-u` option prints only unique lines:

uniq -u kt.txt

prints only unique lines

prints only unique lines

Skipping First N Fields (-f option) Using `uniq` Command in Linux

The `-f N` option skips the first N fields before comparing lines. Useful for numbered lines:

uniq -f 2 f1.txt

//displaying contents of f1.txt// 1. I love music. 2. I love music. 3. I love music of Kartik. 4. I love music of Kartik.

`-s N` option

`-s N` option

In this example , 2 is used cause we needed to compare the lines after the numbering 1,2.. and after dots

Skipping First N Characters (-s option) Using `uniq` Command in Linux

The `-s N` option skips the first N characters in each line:

//displaying content of f2.txt// #%@I love music. ^&(I love music. *-!@thanks. #%@!thanks.

uniq -s 3 f2.txt

In this example lines same after skipping 3 characters are filtered.

`-s N` option

`-s N` option

Limiting Comparison to First N Characters (-w option) Using `uniq` Command in Linux

Using -w option : Similar to the way of skipping characters, we can also ask uniq to limit the comparison to a set number of characters. For this, -w command-line option is used. 

//displaying content of f3.txt// How it is possible? How it can be done? How to use it?

uniq -w 3 f3.txt

As the first 3 characters of all the 3 lines are same that’s why uniq treated all these as duplicates and gave output accordingly.

Using -w option

Using -w option

Case-Insensitive Comparison (-i option) Using `uniq` Command in Linux

The `-i` option makes the comparison case-insensitive:

//displaying contents of f4.txt// I LOVE MUSIC i love music THANKS

uniq f4.txt

Here lines aren’t treated as duplicates with simple use of uniq //now using -i option//

uniq -i f4.txt

Now second line is removed when -i option is used.

comparison case-insensitive

comparison case-insensitive

NULL Terminated Output (-z option) Using `uniq` Command in Linux

Using -z option : By default, the output uniq produces is newline terminated. However, if you want, you want to have a NULL terminated output instead (useful while dealing with uniq in scripts). This can be made possible using the -z command line option. 

Syntax: 

uniq -z file-name

Conclusion

In this article we discussed the uniq command in Linux is a versatile tool for handling duplicate lines in text files. By understanding its various options, you can efficiently manage and process text data. Experiment with different options and examples to master the usage of the uniq command.
 

?list=PLqM7alHXFySFc4KtwEZTANgmyJm3NqS_L 

uniq Command in Linux with Examples – FAQs

What is the uniq command in Linux?

The uniq command in Linux is used to filter out or report repeated lines in a file. It is commonly used in conjunction with the sort command because uniq only detects duplicate lines that are adjacent. Therefore, for the most effective use, lines should be sorted before using uniq.

How to remove duplicate lines using uniq?

To remove duplicate lines using uniq, you should first sort the file and then pipe the output to uniq. Here’s how you can do it:

sort filename.txt | uniq

This command sequence sorts the file and then removes any adjacent duplicate lines.

Can uniq command be used with sorted files?

Yes, the uniq command is most effective when used with sorted files because it only removes or identifies duplicate lines that are directly adjacent to each other. If the file is not sorted, duplicates that are not consecutive will not be detected or removed.

How to count duplicate lines with uniq?

To count duplicate lines using the uniq command, use the -c option, which prefixes lines by the number of occurrences. Here’s how to do it:

sort filename.txt | uniq -c

This command will sort the file and then display the count of each unique line.

How to ignore case differences with uniq command?

To ignore case differences when using the uniq command, you can use the -i option. This will treat lines that differ in case as duplicates. Here’s an example:

sort filename.txt | uniq -i


Similar Reads

Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
comm command in Linux with examples
comm compare two sorted files line by line and write to standard output; the lines that are common and the lines that are unique. Suppose you have two lists of people and you are asked to find out the names available in one and not in the other, or even those common to both. comm is the command that will help you to achieve this. It requires two so
5 min read
cmp Command in Linux with examples
cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you to find out whether the two files are identical or not. When cmp is used for comparison between two files, it reports the location of the first mismatch to the screen if difference is found and if no difference is found i.e the files compared are identical.cmp dis
5 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
df Command in Linux with examples
There might come a situation while using Linux when you want to know the amount of space consumed by a particular file system on your LINUX system or how much space is available on a particular file system. LINUX being command friendly provides a command line utility for this i.e df command that displays the amount of disk space available on the fi
8 min read
help Command in Linux with examples
If you are new to LINUX operating system and having trouble dealing with the command-line utilities provided by LINUX then you really need to know first of all about the help command which as its name says help you to learn about any built-in command. help command as told before just displays information about shell built-in commands. Here's the sy
3 min read
fgrep command in Linux with examples
The fgrep filter is used to search for the fixed-character strings in a file. There can be multiple files also to be searched. This command is useful when you need to search for strings which contain lots of regular expression metacharacters, such as "^", "$", etc. Syntax: fgrep [options] [ -e pattern_list] [pattern] [file] Options with Description
3 min read
three90RightbarBannerImg