Linux bash provides a lot of useful commands where the sort
command is one of them. As its name suggests the sort command is used to sort different content from a file, command, device in different ways. The default behavior of the sort command is sorting provided content from a to Z in ASCII format.
Bash sort Command Use Cases
Bash sort command can be used in different cases and scenarios. Below we list some of the most populars.
- The sort command can be used to sort content of a text file.
- The sort command can be used to sort output of a command.
- The sort command can be used to sort two contanated files content.
- The sort command can be used to sort the response of a web and network request.
- The sort command can be used to sort a bash script output.
Sample Text
During this tutorial about sort command we will use the following content which is stored in the file names cities
.
london ankara istanbul newyork dublin berlin
$ cat cities.txt
Sort File Cotent
Lets start with a simple example. We will sort the content of the file named cities.txt
.
$ sort cities.txt
From the output we can see that the sort command works as caseinsensitive where the lowercase and uppercase letters are behaved the same. For example the dublin
and Dublin
are the same during sort operation.
Sort Command Output
By defualt the sort command output is printed to the standard output. We can redirect the sort command output into a file by using the redirection operator. In the following example we save the sort command output into the file named sorted_cities.txt
.
$ sort cities.txt > sorted_cities.txt
As the output is redirected into the file named sorted_cities.txt there is no visible output.
Sort Alphabetically
Save Sorted Content Into A File
The sort command output can be written into a file by using the -o
option. Also the file name we want to put should be provided too. In the following example the sorted content is written into the file named sorted_cities.txt
.
$ sort cities.txt -o sorted_cities.txt
Sort In Reverse Order
The sort command sorts the content incrementally. But the sort order can be reversed by using the -r
option. The -r option sorts decrementally where starts sorting from letter z to the a.
$ sort -r cities.txt
Sort Numerically Not Alphabetically
The sort command sorts file content alphabetically and interprets the numbers as letters. But we can sort and interpret file content numerically by using the -n
option.
$ sort -n numbers.txt
Sort Numerically In Reverse Order
As the -n
option is used to sort numerically the file content can be sorted numerically in reverse order. The -r option is used with the -n
option in order to sort numerically in reverse order.
$ sort -n -r numbers.txt
Sort According To Specified Column
A text file may contain multiple columns like name, count etc. The sort column can be used to sort according to the specified column. In the following example we will sort according to the 2nd column by using the -k
option.
$ sort -k 2 cities.txt