Table of Contents
This tip is about the how to Use Sort Command to Sort Text Files in Linux. So read this free guide, How to Use Sort Command to Sort Text Files in Linux step by step. If you have query related to same article you may contact us.
How to Use Sort Command to Sort Text Files in Linux – Guide
The sort command can be used to sort and organize lines in a text file. You have the option of sorting the data into a text file and displaying the results on screen or redirecting it to a file. Sort provides several command-line options for sorting data in a text file, depending on your needs.
Sort command syntax:
$sort [-options]
For example, here is a test file:
$ cat test zzz sss qqq yyy BBB ddd AAA
And, here is what you get when the sort command is run on this file without any options. It sorts the lines in the test file and displays sorted output.
$ sort test yyy AAA BBB ddd qqq sss zzz
1. Run numerical sort using the -n option
If we want to sort by numeric value, we can use the -n or –numeric-sort option.
Create the following test file for this example:
test $ cat 22 zzz 33 sss 11 qqq 77 aaa 55 BBB
The following sort command sorts the lines in the test file at the numeric value in 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. Sort human readable numbers using the -h option
If we want to sort by human readable numbers (eg 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 (ie 1K = 1 mil, 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 a year using the -M option
If we want to sort in the order of the months of the year, we can use the -M or –month-sort option.
Create the following test file for this example:
test $ cat september august january october april feb mar11
The following sort command sorts the lines in the test file according to month order. Note that the lines of the file must contain at least 3 characters of the month name at the beginning of the line (eg jan, feb, mar). If we give it already for January or au for August, the sort command will not consider it as the name of the month.
$sort -M Test Jan Feb Mar11 Apr Aug Sep Oct
4. Check if the content has already been rated using the -c option
If we want to check whether the data in the text file is sorted or not, we can use option -c or -check, -check = diagnosis-first.
Create the following test file for this example:
test $ cat 2 5 1 6
The following sort command checks whether the text file data is sorted or not. If not, it shows the first occurrence with the row number and unordered value.
$ sort -c test sort: test: 3: clutter: 1
5. Reverse the output and verify uniqueness using the -r and -u options
If we want to get the output sorted in reverse order, we can use the -r or –reverse option. If the file contains duplicate lines, to get unique lines in sorted output, option “-u” can be used.
Create the following test file for this example:
test $ cat 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 test file lines in reverse order and removes duplicate lines from sorted output.
$sort -r -u test 5 4 2 1
6. Selectively sort the content, customize the delimiter, and write the output to a file using the -k, -t, -o options
If we want to sort by column or word position in the lines of the text file, then the “-k” option can be used. If each word in each line of the file is separated by a delimiter, except ‘space’, then we can specify the delimiter using the “-t” option. We can get the output sorted into any specified output file (using the “-o” option) instead of displaying the output to standard output.
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 in the test file into the third word of each line and displays the sorted output.
$ sort -k3 test aa aa ff aa aa kk aa aa tt aa aa zz $ test aa | 5th | zz yy | 2nd | ff aa | 1st | tt aa | 3rd | haha
Here, several options are used at the same time. In the test file, words on each line are separated by the ‘|’ delimiter. It sorts the lines in the test file into the 2nd word of each line based on the numeric value and stores the sorted output in the specified output file.
$sort -n -t ‘|’ -k2 test -o outfile
The contents of the output file are shown below.
$cat aa output file | 1st | tt aa | 2nd | ff aa | 3rd | kk yy | 5th | z
Final note
I hope you like the guide How to Use Sort Command to Sort Text Files in Linux. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.