[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
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 use and ad variables on Bash Scrip.
July 12, 2023

How to use and ad variables on Bash Scrip.

How To Use And Ad Variables On Bash Scrip. Bash scripting is a powerful tool for automating tasks in the Linux environment. One of the fundamental concepts in Bash scripting is variables. Variables allow you to store and manipulate data within a script. In this article, we will explore the basics…

Read article
How to create a script in bash.
July 11, 2023

How to create a script in bash.

How To Create A Script In Bash. Bash is a popular scripting language used in Unix-based operating systems. It is known for its simplicity and powerful features. In this article, we will walk you through the process of writing a basic “Hello, World!” script in Bash. Step 1: Create a new Bash script…

Read article
How to install Python 3 in CentOS 7.
July 10, 2023

How to install Python 3 in CentOS 7.

How To Install Python 3 In CentOS 7. This tutorial will guide you through installing Python 3 on a CentOS 7 system using the Software Collections (SCL) alongside the distribution default Python version 2.7. We will also show you how to create a Python virtual environment. Enable Software…

Read article
Get in Touch

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

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

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00