How to use sort command in Linux.

What is sort command?

The `sort` command in Linux is part of the GNU Coreutils package. This command is used to sort lines of text in a specified file or from the standard input in an ascending/descending order. The sorted output can be written to a file or displayed on the standard output. The `sort` command is a simple and powerful tool that can be used in various scenarios, from sorting data in a file for further analysis to preparing data for a report.

The basic syntax of the `sort` command is as follows:

12# Syntax
sort [options] [file(s)]

This command provides various options that can be used to customize the sorting process and file(s) that need to be sorted. If no file is specified, the `sort` command will sort the input from the standard input (i.e., the keyboard).

Here are some frequently used options with the `sort` command in Unix/Linux systems:

  • -r: sort the input in reverse order.

  • -n: sort the input numerically.

  • -k: sort the input based on a specific field or column.

  • -b: ignore the leading blanks.

  • -t: specify the field separator.

  • -u: remove duplicate lines from the output.

  • -o: specify the output file.

Let’s take a closer look at each of these options with some examples.

 

Sorting File Content

The default content is sorted in ascending order basis on the first character. You can simply type the `sort` command followed by the file name.

sort data.txt 

You can also use the following commands to provide input to the command.

sort < data.txt 
cat data.txt | sort 

 

Sorting in Reverse Order

The -r option is used to sort the input in reverse order. Here’s an example of how to use this option:

sort -r data.txt 

This command sorts the lines of text in the data.txt file in reverse order and displays the result on the screen.

 

Numerical Sorting

The -n option is used to sort the input numerically. This option is useful when sorting numbers that are represented as text. Here’s an example of how to use this option:

sort -n data.txt 

This command sorts the lines of text in the data.txt file numerically and displays the result on the screen.

 

Sorting by Field

The -k option is used to sort the input based on a specific field or column. This option is useful when sorting tabular data where each line represents a record and fields are separated by a specific delimiter. The field number is specified with the option, and the fields are numbered starting from 1. Here’s an example of how to use this option:

sort -k 2 data.txt 

This command sorts the lines of text in the data.txt file based on the second field (column) and displays the result on the screen.

 

Specifying the Field Separator

The -t option is used to specify the field separator when sorting by field. By default, the `sort` command assumes that the fields are separated by a space or tab character. However, this option allows you to specify a different character as the field separator. Here’s an example of how to use this option:

sort -t "," -k 2 data.txt 

This command sorts the lines of text in the data.txt file based on the second field (column) and the field separator is ‘,’. It will display the result on the screen

 

Removing Duplicate Lines

The -u option is used to remove duplicate lines from the output of the `sort` command. This option is useful when sorting a large file and you only want to keep unique lines. Here’s an example of how to use this option:

sort -u data.txt 

This command sort the lines of text in the data.txt file and remove any duplicate lines from the output. The result is displayed on the screen.

 

Specifying the Output File

The -o option is used to specify the output file for the sorted data. This option is useful when saving the sorted data to a different file rather than displaying it on the screen. Here’s an example of how to use this option:

sort data.txt -o sorted_data.txt 

Instead of the option, you can also use the redirect “>” operator to write content to the file.

sort data.txt > sorted_data.txt 

This command sorts the lines of text in the data.txt file and saves the result to a new file called sorted_data.txt.

 

Combining Multiple Options

You can combine different options to achieve the desired sorting result. For example, to sort a file numerically in reverse order and save the result to a new file, you can use the following command:

sort -nr data.txt -o reverse_sorted_data.txt 

You can also combine different options like this,

sort -t "," -k 2,3 -u data.txt 

This command sorts the lines of text in the “data.txt” file based on the second and third field (column) and the field separator is ‘,’ and removes duplicate entries from the result and displays it on the screen

 

Conclusion

In conclusion, the `sort` command is a powerful and versatile tool that can be used to sort data in various scenarios. The various options available with the `sort` command allows you to customize the sorting process and achieve the desired result. With the examples and explanations provided in this article, you should have a good understanding of how to use the `sort` command and be able to apply it to your own data.