14 Useful Examples of Linux ‘sort’ Command
Sort is a Linux program used for printing lines of input text files and concatenation of all files in sorted order. Sort command takes blank space as field separator and entire Input file as sort key. It is important to notice that sort command don’t actually sort the files but only print the sorted output, until your redirect the output.
This article aims at deep insight of Linux ‘sort‘ command with 14 useful practical examples that will show you how to use sort command in Linux.
1. First we will be creating a text file (tecmint.txt) to execute ‘sort‘ command examples. Our working directory is ‘/home/$USER/Desktop/tecmint.
The option ‘-e‘ in the below command enables interpretion of backslash and /n tells echo to write each string to a new line.
$ echo -e "computer\nmouse\nLAPTOP\ndata\nRedHat\nlaptop\ndebian\nlaptop" > tecmint.txt
2. Before we start with ‘sort‘ lets have a look at the contents of the file and the way it look.
$ cat tecmint.txt
3. Now sort the content of the file using following command.
$ sort tecmint.txt
Note: The above command don’t actually sort the contents of text file but only show the sorted output on terminal.
4. Sort the contents of the file ‘tecmint.txt‘ and write it to a file called (sorted.txt) and verify the content by usingcat command.
$ sort tecmint.txt > sorted.txt $ cat sorted.txt
5. Now sort the contents of text file ‘tecmint.txt‘ in reverse order by using ‘-r‘ switch and redirect output to a file ‘reversesorted.txt‘. Also check the content listing of the newly created file.
$ sort -r tecmint.txt > reversesorted.txt $ cat reversesorted.txt
6. We are going a create a new file (lsl.txt) at the same location for detailed examples and populate it using the output of ‘ls -l‘ for your home directory.
$ ls -l /home/$USER > /home/$USER/Desktop/tecmint/lsl.txt $ cat lsl.txt
Now will see examples to sort the contents on the basis of other field and not the default initial characters.
7. Sort the contents of file ‘lsl.txt‘ on the basis of 2nd column (which represents number of symbolic links).
$ sort -nk2 lsl.txt
Note: The ‘-n‘ option in the above example sort the contents numerically. Option ‘-n‘ must be used when we wanted to sort a file on the basis of a column which contains numerical values.
8. Sort the contents of file ‘lsl.txt‘ on the basis of 9th column (which is the name of the files and folders and is non-numeric).
$ sort -k9 lsl.txt
9. It is not always essential to run sort command on a file. We can pipeline it directly on the terminal with actual command.
$ ls -l /home/$USER | sort -nk5
10. Sort and remove duplicates from the text file tecmint.txt. Check if the duplicate has been removed or not.
$ cat tecmint.txt $ sort -u tecmint.txt
Rules so far (what we have observed):
- Lines starting with numbers are preferred in the list and lies at the top until otherwise specified (-r).
- Lines starting with lowercase letters are preferred in the list and lies at the top until otherwise specified (-r).
- Contents are listed on the basis of occurrence of alphabets in dictionary until otherwise specified (-r).
- Sort command by default treat each line as string and then sort it depending upon dictionary occurrence of alphabets (Numeric preferred; see rule – 1) until otherwise specified.
11. Create a third file ‘lsla.txt‘ at the current location and populate it with the output of ‘ls -lA‘ command.
$ ls -lA /home/$USER > /home/$USER/Desktop/tecmint/lsla.txt $ cat lsla.txt
Those having understanding of ‘ls‘ command knows that ‘ls -lA’=’ls -l‘ + Hidden files. So most of the contents on these two files would be same.
12. Sort the contents of two files on standard output in one go.
$ sort lsl.txt lsla.txt
Notice the repetition of files and folders.
13. Now we can see how to sort, merge and remove duplicates from these two files.
$ sort -u lsl.txt lsla.txt
Notice that duplicates has been omitted from the output. Also, you can write the output to a new file by redirecting the output to a file.
14. We may also sort the contents of a file or the output based upon more than one column. Sort the output of ‘ls -l‘ command on the basis of field 2,5 (Numeric) and 9 (Non-Numeric).
$ ls -l /home/$USER | sort -t "," -nk2,5 -k9
15 examples of sort command in Linux
sort command is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ascii. Using options in sort command, it can also be used to sort numerically. Let us discuss it with some examples:
File with Ascii data:
Let us consider a file with the following contents:
$ cat file Unix Linux Solaris AIX Linux HPUX
1. sort simply sorts the file in alphabetical order:
$ sort file AIX HPUX Linux Linux Solaris Unix
All records are sorted alphabetically.
2. sort removes the duplicates using the -u option:
$ sort -u file AIX HPUX Linux Solaris Unix
The duplicate ‘Linux’ record got removed. ‘-u’ option removes all the duplicate records in the file. Even if the file have had 10 ‘Linux’ records, with -u option, only the first record is retained.
File with numbers:
Let us consider a file with numbers:
$ cat file 20 19 5 49 200
3. The default sort ‘might’ give incorrect result on a file containing numbers:
$ sort file 19 20 200 49 5
In the above result, 200 got placed immediately below 20, not at the end which is incorrect. This is because the sort did ASCII sort. If the file had not contained ‘200’, the default sort would have given proper result. However, it is incorrect to sort a numerical file in this way since the sorting logic is incorrect.
4. To sort a file numericallly:
$ sort -n file 5 19 20 49 200
-n option can sort the decimal numbers as well.
5. sort file numerically in reverse order:
$ sort -nr file 200 49 20 19 5
‘r’ option does a reverse sort.
Multiple Files:
Let us consider examples with multiple files, say file1 and file2, containing numbers:
$ cat file1 20 19 5 49 200
$ cat file2 25 18 5 48 200
6. sort can sort multiple files as well.
$ sort -n file1 file2 5 5 18 19 20 25 48 49 200 200
The result of sort with multiple files will be a sorted and merged output of the multiple files.
7. Sort, merge and remove duplicates:
$ sort -nu file1 file2 5 18 19 20 25 48 49 200
-u option becomes more handy in case of multiple files. With this, the output is now sorted, merged and without duplicate records.
Files with multiple fields and delimiter:
Let us consider a file with multiple fields:
$ cat file Linux,20 Unix,30 AIX,25 Linux,25 Solaris,10 HPUX,100
8. sorting a file containing multiple fields:
$ sort file AIX,25 HPUX,100 Linux,20 Linux,25 Solaris,10 Unix,30
As shown above, the file got sorted on the 1st field, by default.
9. sort file on the basis of 1st field:
$ sort -t"," -k1,1 file AIX,25 HPUX,100 Linux,20 Linux,25 Solaris,10 Unix,30
This is being more explicit. ‘-t’ option is used to provide the delimiter in case of files with delimiter. ‘-k’ is used to specify the keys on the basis of which the sorting has to be done. The format of ‘-k’ is : ‘-km,n’ where m is the starting key and n is the ending key. In other words, sort can be used to sort on a range of fields just like how the group by in sql does. In our case, since the sorting is on the 1st field alone, we speciy ‘1,1’. Similarly, if the sorting is to be done on the basis of first 3 fields, it will be: ‘-k 1,3’.
Note: For a file which has fields delimited by a space or a tab, there is no need to specify the “-t” option since the white space is the delimiter by default in sort.
10. sorting file on the basis of the 2nd field:
$ sort -t"," -k2,2 file Solaris,10 HPUX,100 Linux,20 AIX,25 Linux,25 Unix,30
11. sorting file on the basis of 2nd field , numerically:
$ sort -t"," -k2n,2 file Solaris,10 Linux,20 AIX,25 Linux,25 Unix,30 HPUX,100
12. Remove duplicates from the file based on 1st field:
$ sort -t"," -k1,1 -u file AIX,25 HPUX,100 Linux,20 Solaris,10 Unix,30
The duplicate Linux record got removed. Keep in mind, the command “sort -u file” would not have worked here becuase both the ‘Linux’ records are not same, the values were different. However, in the above, sort is told to remove the duplicates based on the 1st key, and hence the duplicate ‘Linux’ record got removed. According to sort, in case of a group of similar records, except the first one, the rest are considered duplicate.
13. Sort the file numerically on the 2nd field in reverse order:
$ sort -t"," -k2nr,2 file HPUX,100 Unix,30 AIX,25 Linux,25 Linux,20 Solaris,10
14. sort the file alphabetically on the 1st field, numerically on the 2nd field:
$ sort -t"," -k1,1 -k2n,2 file AIX,25 HPUX,100 Linux,20 Linux,25 Solaris,10 Unix,30
15. sort a file based on the 1st and 2nd field, and numerically on 3rd field on a file containing 5 columns:
$ sort -t"," -k1,2 -k3n,3 file