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

uniq Command in Linux with Examples

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. 

Table of Content

  • Syntax of uniq Command
  • Common Options of the uniq Command
  • Examples of Using the `uniq` Command
  • How to Remove Duplicate Lines Using `uniq` Command
  • How to Count Duplicate Lines Using `uniq` Command in Linux
  • How to Display Repeated Lines Using `uniq` Command in Linux
  • How to Display all Duplicate Lines Using `uniq` Command in Linux
  • How to Display Unique Lines Using `uniq` Command in Linux
  • Skipping First N Characters (-s option) Using `uniq` Command in Linux
  • Limiting Comparison to First N Characters (-w option) Using `uniq` Command in Linux
  • Case-Insensitive Comparison (-i option) Using `uniq` Command in Linux
  • NULL Terminated Output (-z option) Using `uniq` Command in Linux

Similar Reads

Syntax of uniq Command

The basic syntax of the `uniq` command is:...

Common Options of the uniq Command

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

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....

How to Remove Duplicate Lines Using `uniq` Command

To remove duplicate lines from `kt.txt`, we can use the `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:...

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

The `-d` option only prints 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:...

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

The `-u` option 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:...

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

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

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....

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

The `-i` option makes the 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....

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....