Back
You can sort the contents of a file in a number of ways. By default, the sort command sorts the contents in alphabetical order depending on the first letter in each line.Sort command has the capability of sorting numerical values and strings. The sort command can order the lines in a text file.
For example :
The sort /etc/passwd command would sort all users ( including those associated with specific services and such ) by username.
Before practicing the examples create the below two files in your Linux system:
$ cat order.txt
Unix distributed 05 server
Linux virtual 3 server
Unix distributed 05 server
Distributed processing 6 system
$ cat delim_sort.txt
Mayday|4
Janmon|1
Declast|12
1. Sorting lines of text
The default sort command uses alphabetical order (ASCII order) to sort the file. It treats each line as a string and then sorts the lines.
$ sort order.txt
Distributed processing 6 system
Linux virtual 3 server
Unix distributed 05 server
Unix distributed 05 server
2. Sorting based on the field positions.
You can specify the field postions using the -k option of sort command. The sort command uses the space or tab as the default delimiter. To sort based on the data in the second field, run the below command:
$ sort -k2 order.txt
Unix distributed 05 server
Unix distributed 05 server
Distributed processing 6 system
Linux virtual 3 server
You can also specify more than field with k option as a comma separated list. The below command uses the second and fourth fields to sort the data.
$ sort -k2,4 order.txt
3. Numeric sorting
Instead of the default alphabetical sorting order, you can make the sort command to sort in numeric order using the -n option. This is shown below:
$ sort -nk3 order.txt
Linux virtual 3 server
Unix distributed 05 server
Unix distributed 05 server
Distributed processing 6 system
4. Sort in reverse order
By default, the sort command sorts the data in ascending order. You can change this to descending order using the -r option.
$ sort -nrk3 order.txt
Distributed processing 6 system
Unix distributed 05 server
Unix distributed 05 server
Linux virtual 3 server
5. Suppressing duplicates or Print only unique values
You can produce only unique values in the output using the - u option of the sort command.
$ sort -u order.txt
Distributed processing 6 system
Linux virtual 3 server
Unix distributed 05 server
Another way is piping the output of sort command to uniq command.
$ sort order.txt | uniq
6. Delimited file input
In the second, third and fourth examples we have sorted the data based on the field positions. Here the fields are separted by space or tab character. What if the fields are specifed by any other character? In such cases, we have to specify the input delimiter with the -t option. An example is shown below:
$ sort -t'|' -nrk2 delim_sort.txt
Declast|12
Mayday|4
Janmon|1
7. Sorting on months.
We can sort the data in the monthwise using the -M option of the sort command. This is shown below:
$ sort -M delim_sort.txt
Janmon|1
Mayday|4
Declast|12
Treats the first 3 characters in the string as month and then sorts in months order.
You can sort the contents of a file in a number of ways. By default, the sort command sorts the contents in alphabetical order depending on the first letter in each line.Sort command has the capability of sorting numerical values and strings. The sort command can order the lines in a text file.
Syntax :
sort [options] file-nameFor example :
The sort /etc/passwd command would sort all users ( including those associated with specific services and such ) by username.
Before practicing the examples create the below two files in your Linux system:
$ cat order.txt
Unix distributed 05 server
Linux virtual 3 server
Unix distributed 05 server
Distributed processing 6 system
$ cat delim_sort.txt
Mayday|4
Janmon|1
Declast|12
1. Sorting lines of text
The default sort command uses alphabetical order (ASCII order) to sort the file. It treats each line as a string and then sorts the lines.
$ sort order.txt
Distributed processing 6 system
Linux virtual 3 server
Unix distributed 05 server
Unix distributed 05 server
2. Sorting based on the field positions.
You can specify the field postions using the -k option of sort command. The sort command uses the space or tab as the default delimiter. To sort based on the data in the second field, run the below command:
$ sort -k2 order.txt
Unix distributed 05 server
Unix distributed 05 server
Distributed processing 6 system
Linux virtual 3 server
You can also specify more than field with k option as a comma separated list. The below command uses the second and fourth fields to sort the data.
$ sort -k2,4 order.txt
3. Numeric sorting
Instead of the default alphabetical sorting order, you can make the sort command to sort in numeric order using the -n option. This is shown below:
$ sort -nk3 order.txt
Linux virtual 3 server
Unix distributed 05 server
Unix distributed 05 server
Distributed processing 6 system
4. Sort in reverse order
By default, the sort command sorts the data in ascending order. You can change this to descending order using the -r option.
$ sort -nrk3 order.txt
Distributed processing 6 system
Unix distributed 05 server
Unix distributed 05 server
Linux virtual 3 server
5. Suppressing duplicates or Print only unique values
You can produce only unique values in the output using the - u option of the sort command.
$ sort -u order.txt
Distributed processing 6 system
Linux virtual 3 server
Unix distributed 05 server
Another way is piping the output of sort command to uniq command.
$ sort order.txt | uniq
6. Delimited file input
In the second, third and fourth examples we have sorted the data based on the field positions. Here the fields are separted by space or tab character. What if the fields are specifed by any other character? In such cases, we have to specify the input delimiter with the -t option. An example is shown below:
$ sort -t'|' -nrk2 delim_sort.txt
Declast|12
Mayday|4
Janmon|1
7. Sorting on months.
We can sort the data in the monthwise using the -M option of the sort command. This is shown below:
$ sort -M delim_sort.txt
Janmon|1
Mayday|4
Declast|12
Treats the first 3 characters in the string as month and then sorts in months order.