7 Interesting Linux 'Sort' Command Examples - Part 2
7 Interesting Linux 'Sort' Command Examples - Part 2
com/linux-sort-command-examples/
Linux Foundation LFCS and LFCE Certi�cation Preparation Guide - Get This Book
Menu
Menu
Editor Last Updated: April 18, 2015 Linux Commands Leave a comment
In our last article we have covered various examples on sort command, if you’ve missed, you can go
through it using below link. In continuation to the last post this post aims at covering remaining of
sort command so that both the article together serves as complete guide to Linux ‘sort‘ command.
Before we continue further, create a text �le ‘month.txt‘ and populate it with the data as given below.
1 of 26 1/21/21, 7:54 PM
7 Interesting Linux 'sort' Command Examples - Part 2 https://www.tecmint.com/linux-sort-command-examples/
15. Sort the �le ‘month.txt‘ on the basis of month order by using switch ‘M‘ (–month-sort).
$ sort -M month.txt
Important: Note that ‘sort‘ command needs at least 3 characters to consider month name.
2 of 26 1/21/21, 7:54 PM
7 Interesting Linux 'sort' Command Examples - Part 2 https://www.tecmint.com/linux-sort-command-examples/
16. Sort the data that is in human readable format say 1K, 2M, 3G, 2T, where K,M,G,T represents Kilo,
Mega, Giga, Tera.
3 of 26 1/21/21, 7:54 PM
7 Interesting Linux 'sort' Command Examples - Part 2 https://www.tecmint.com/linux-sort-command-examples/
17. In the last article we have created a �le ‘sorted.txt‘ in example number 4 and another text �le
‘lsl.txt‘ in example number 6. We know ‘sorted.txt‘ is already sorted while ‘lsl.txt‘ is not. Lets check
both the �les are sorted or not using sort command.
$ sort -c sorted.txt
4 of 26 1/21/21, 7:54 PM
7 Interesting Linux 'sort' Command Examples - Part 2 https://www.tecmint.com/linux-sort-command-examples/
$ sort -c lsl.txt
5 of 26 1/21/21, 7:54 PM
7 Interesting Linux 'sort' Command Examples - Part 2 https://www.tecmint.com/linux-sort-command-examples/
18. If the delimiter (separator) between words are space, sort command automatically interpret
anything after horizontal space as new word. What if the delimiter is not space?
Consider a text �le, the contents of which are separated by anything other than space such as ‘|’ or ‘\’
or ‘+’ or ‘.’ or….
6 of 26 1/21/21, 7:54 PM
7 Interesting Linux 'sort' Command Examples - Part 2 https://www.tecmint.com/linux-sort-command-examples/
$ echo -e "21+linux+server+production\n11+debian+RedHat+CentOS\n131+Apache+Mysql+PHP
$ cat delimiter.txt
Now sort this �le on the basis of 1st �eld which is numerical.
7 of 26 1/21/21, 7:54 PM
7 Interesting Linux 'sort' Command Examples - Part 2 https://www.tecmint.com/linux-sort-command-examples/
8 of 26 1/21/21, 7:54 PM
7 Interesting Linux 'sort' Command Examples - Part 2 https://www.tecmint.com/linux-sort-command-examples/
If the delimiter is Tab you may use $’\t’ in place of ‘+’, as shown in the above example.
19. Sort the contents of ‘ls -l‘ command for your home directory on the basis of 5th column which
represents the ‘amount of data‘ in Random order.
9 of 26 1/21/21, 7:54 PM
7 Interesting Linux 'sort' Command Examples - Part 2 https://www.tecmint.com/linux-sort-command-examples/
Every time you run the above piece of script you are likely to get a different result since the result is
generated randomly.
As clear from the Rule number – 2 from the last article, sort command prefer line starting with
lowercase characters over uppercase characters. Also check example 3 in last article, where string
‘laptop‘ appears before string ‘LAPTOP‘.
20. How to override the default sorting preference? before we are able to override the default sorting
. To do this run the below code on
10 of 26 1/21/21, 7:54 PM
7 Interesting Linux 'sort' Command Examples - Part 2 https://www.tecmint.com/linux-sort-command-examples/
$ export LC_ALL=C
And then sort the text �le ‘tecmint.txt‘ overriding the default sort preference.
$ sort tecmint.txt
11 of 26 1/21/21, 7:54 PM
7 Interesting Linux 'sort' Command Examples - Part 2 https://www.tecmint.com/linux-sort-command-examples/
$ sort -f tecmint.txt
21. How about running ‘sort‘ on two input �les and join them in one go!
Lets create two text �le namely ‘�le1.txt‘ and ‘�le2.txt‘ and populate it with some data. Here we are
populating ‘�le1.txt‘ with numbers as below. Also used ‘cat‘ command to check the content of �le.
source\n4 customizable” > file1.txt
12 of 26 1/21/21, 7:54 PM
7 Interesting Linux 'sort' Command Examples - Part 2 https://www.tecmint.com/linux-sort-command-examples/
$ cat file1.txt
13 of 26 1/21/21, 7:54 PM
7 Interesting Linux 'sort' Command Examples - Part 2 https://www.tecmint.com/linux-sort-command-examples/
14 of 26 1/21/21, 7:54 PM