What does the -u option do in the sort command?

The -u option in the sort command is used to sort the input and output only the unique lines, effectively removing any duplicate lines from the result. When you use -u, the command will only display one instance of each line, even if that line appears multiple times in the input.

Example

Assuming you have a file named data.txt with the following content:

apple
banana
apple
cherry
banana
date

If you run the command:

sort -u data.txt

The output will be:

apple
banana
cherry
date

Explanation

  • sort -u data.txt: This command sorts the lines in data.txt and removes duplicates, displaying only unique lines in the sorted order.

Using the -u option is a convenient way to both sort and filter out duplicate entries in one step.

0 Comments

no data
Be the first to share your comment!