[email protected]Office hours: Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedOctober 10, 2023

How to use uniq command in Linux.

How To Use Uniq Command In Linux.

Linux command-line tools offer a wide range of functionality to help users manage and manipulate text data efficiently. One such utility is the uniq command, which is used for identifying and removing duplicate lines from text files. In this article, we will explore the various aspects of the uniq command and provide numerous examples to illustrate its usage.

 

Basic Syntax

The basic syntax of the uniq command is as follows:

uniq [OPTION]... [INPUT [OUTPUT]]
  • OPTION: Specifies various options to customize the behavior of the command.

  • INPUT: Specifies the input file to process. If not provided, the command reads from the standard input.

  • OUTPUT: Specifies the output file where the results are written. If not provided, the results are displayed on the standard output.

 

Common Options

The uniq command offers several options that allow you to control its behavior. Here are some of the most commonly used options:

  • -d, –repeated: Display only duplicate lines.

  • -u, –unique: Display only unique lines (non-duplicate lines).

  • -c, –count: Prefix lines with the count of occurrences.

  • -i, –ignore-case: Ignore differences in case (treat uppercase and lowercase characters as the same).

  • -f N, –skip-fields=N: Skip the first N fields before checking for duplicates (fields are separated by whitespace).

  • -s N, –skip-chars=N: Skip the first N characters before checking for duplicates.

 

Examples

Let’s explore some practical examples to understand how to use the uniq command effectively:

 

Example 1: Basic Usage

Suppose we have a file named fruits.txt with the following content:

apple
banana
apple
orange
banana

 

Example 2: Displaying Unique Lines

To display only the unique lines (i.e., non-duplicate lines) from a file, use the -u option:

uniq -u fruits.txt

The output will be:

orange

 

Example 3: Counting Duplicate Lines

To count the number of occurrences of each line in a file, use the -c option:

uniq -c fruits.txt

The output will be:

2 apple
2 banana
1 apple
1 orange
1 banana

 

Example 4: Ignoring Case

To perform a case-insensitive comparison when identifying duplicates, use the -i option:

uniq -i fruits.txt

The output will be:

apple
orange
banana

 

Example 5: Skipping Fields

If your file has multiple fields separated by whitespace, you can use the -f option to skip a specified number of fields before checking for duplicates. Consider a file named data.txt with the following content:

John Doe 123
Jane Smith 456
John Doe 789

To identify unique lines based on the first field, you can use:

uniq -f 1 data.txt

The output will be:

John Doe 123
Jane Smith 456

 

Conclusion

The uniq command is a valuable tool for identifying and manipulating duplicate and unique lines in text files. With its various options, you can customize its behavior to suit your specific needs. Whether you’re dealing with lists of data, log files, or any text-based content, uniq can help you efficiently manage and process your data on the Linux command line.

Keep reading
How to Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, colocation, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Office hours: Mon – Fri 9:00AM – 5:00PM