Linux and Unix sort command tutorial with examples

Command sort is used to display/arrange the content of the text file in ascending or descending order.
By default, sort command sorts the entire lines.

Syntax:

Example:

Suppose we have a file Employee.txt as shown below and we want to sort the content of the file and display at the terminal. By default sort command sorts the entire lines.

File 1: Employee.txt

Output

Options of sort command

Primary key (-k): Option (–k) is used to sort specific field. Suppose we want to perform shorting according to second field then the command is as follows:

Output:

NOTE: Option –t is used to specify delimiter. Here in the above file Employee.txt, we have used “|” as a delimiter. We can also other delimiters.

Numeric Sort (-n): If the file contains a numeric value and we want to perform shorting on a specific field contains numeric data then –n option is used.
Suppose we want to sort 3rd field of file Employee.txt contains numeric data the command is as follows:

Output

Reverse the Order (-r): Option –r is used to reverse the order of short option.

Other options of sort command:

[table id=8 /]