Arrange files neatly! A very detailed tutorial on how to sort file contents using the Sort command in Linux
Link: https://bbs.huaweicloud.com/blogs/365741
Sort command helps in sorting/ordering the lines in a text file. You can sort the data in a text file and display the output on the screen or redirect it to a file. Depending on your requirement, sort provides several command line options for sorting the data in a text file.
Sort command helps to sort/order the lines in a text file. You can sort the data in a text file and display the output on the screen or redirect it to a file. As per your requirement, sort provides several command line options for sorting the data in a text file.
Sort Command Syntax:
$ sort [-options]
For example, here is a test file:
$ cat test
zzz
sss
qqq
aaa
BBB
ddd
AAA
Moreover, this is the result we get when we execute the sort command on this file without any options. It sorts the lines in the test file and displays the sorted output.
$ sort test
aaa
AAA
BBB
ddd
qqq
sss
zzz
1. Use the -n option to perform a numeric sort
If we want to sort the numeric values, then we can use the -n or --numeric-sort option.
Create the following test file for this example:
$ cat test
22 zzz
33 sss
11 qqq
77 aaa
55 BBB
The following sort command sorts the lines in the test file based on the numerical value of the first word of the line and displays the sorted output.
$ sort -n test
11 qqq
22 zzz
33 sss
55 BBB
77 aaa
2. Use -h option to sort human readable numbers
If we want to sort human readable numbers (for example, 2K 1M 1G), then we can use the -h or --human-numeric-sort option.
Create the following test file for this example:
$ cat test
2K
2G
1K
6T
1T
1G
2M
The following sort command sorts the human-readable numbers (i.e., 1K = 1 thousand, 1M = 1 million, 1G = 1 Giga, 1T = 1 Tera) in the test file and displays the sorted output.
$ sort -h test
1K
2K
2M
1G
2G
1T
6T
3. Sort the months of the year using the -M option
If we want to sort in order of month of the year, then we can use the -M or --month-sort option.
Create the following test file for this example:
$ cat test
sept
aug
jan
oct
apr
feb
mar11
The following sort command sorts the lines in the test file in monthly order. Note that the lines in the file should contain month names of at least 3 characters at the beginning of the line (e.g. jan, feb, mar). If we give ja for January or au for August, then the sort command will not consider it as a month name.
$ sort -M test
jan
feb
mar11
apr
aug
sept
oct
4. Use the -c option to check if the content is sorted
If we want to check whether the data in the text file is sorted, then we can use the -c or --check, --check=diagnose-first option.
Create the following test file for this example:
$ cat test
2
5
1
6
The following sort command checks if the text file data is sorted. If not, it displays the line number of the first occurrence and out-of-order values.
$ sort -c test
sort: test:3: disorder: 1
5. Use -r and -u options to reverse the output and check for uniqueness
If we want to get the sorted output in reverse order, then we can use -r or –reverse option. If the file contains duplicate lines, then to get unique lines in the sorted output, you can use “-u” option.
Create the following test file for this example:
$ cat test
5
2
2
1
4
4
The following sort command sorts the lines in the test file in reverse order and displays the sorted output.
$ sort -r test
5
4
4
2
2
1
The following sort command sorts the lines in the test file in reverse order and removes duplicate lines from the sorted output.
$ sort -r -u test
5
4
2
1
6. Optionally sort the content, customize the delimiter, and write the output to a file using the -k, -t, -o options
If we want to sort the columns or word positions in the text file, then we can use the “-k” option. If each word in each line of our file is separated by a delimiter, other than “space”, then we can specify the delimiter using the “-t” option. Instead of displaying the output on the standard output, we can get the sorted output in any specified output file (using the “-o” option).
Create the following test file for this example:
$ cat test
aa aa zz
aa aa ff
aa aa tt
aa aa kk
The following sort command sorts the lines starting with the third word in each line in the test file and displays the sorted output.
$ sort -k3 test
aa aa ff
aa aa kk
aa aa tt
aa aa zz
$ cat test
aa|5a|zz
aa|2a|ff
aa|1a|tt
aa|3a|kk
Here, several options are used together. In the test file, the words in each line are separated by the delimiter "|". It sorts the lines of the 2nd word in each line in the test file based on the numerical value and stores the sorted output into the specified output file.
$ sort -n -t'|' -k2 test -o outfile
The content of the output file is as follows.
$ cat outfile
aa|1a|tt
aa|2a|ff
aa|3a|kk
aa|5a|zz
Autumn
The recruitment has already begun. If you are not well prepared,
Autumn
It's hard to find a good job.
Here is a big employment gift package for everyone. You can prepare for the spring recruitment and find a good job!