What is the use of -d option in uniq command?

The Use of the -d Option in the uniq Command

The uniq command in Linux is used to filter out duplicate lines from a given input. The -d option in the uniq command is specifically used to display only the duplicate lines in the input.

Understanding the uniq Command

The uniq command takes an input file or stream and outputs a filtered version of the input, where consecutive duplicate lines are replaced by a single line. This is useful for cleaning up data that may contain redundant information.

Here's an example of using the uniq command:

$ cat file.txt
apple
banana
apple
cherry
banana

Running the uniq command on this file would output:

apple
banana
cherry
banana

As you can see, the duplicate lines "apple" and "banana" have been removed, and only the unique lines are displayed.

The -d Option in uniq

The -d option in the uniq command is used to display only the duplicate lines in the input. This can be useful when you want to identify which lines in your data are repeated and need to be addressed.

Here's an example of using the -d option:

$ cat file.txt
apple
banana
apple
cherry
banana

Running the uniq -d command on this file would output:

apple
banana

This shows that the lines "apple" and "banana" are the only duplicate lines in the input.

Mermaid Diagram: uniq Command with -d Option

graph TD A[Input File] --> B[uniq Command] B --> C{Check for Duplicates} C -->|Duplicates Found| D[Display Duplicate Lines] C -->|No Duplicates| E[Display Unique Lines] D --> F[Output] E --> F[Output]

The Mermaid diagram above illustrates the flow of the uniq command with the -d option. The input file is passed to the uniq command, which then checks for duplicate lines. If duplicates are found, the -d option instructs the command to display only the duplicate lines in the output. If no duplicates are found, the command displays the unique lines.

Real-World Example: Identifying Duplicate Email Addresses

Imagine you have a list of email addresses, and you want to identify which addresses are duplicates. You can use the uniq -d command to achieve this.

$ cat emails.txt
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

Running uniq -d emails.txt would output:

[email protected]

This shows that the email address "[email protected]" is a duplicate in the input file.

In conclusion, the -d option in the uniq command is a useful tool for identifying and displaying duplicate lines in a given input. It can be particularly helpful when working with data that may contain redundant information and you need to clean it up.

0 Comments

no data
Be the first to share your comment!