7 Interesting ‘sort’ Command Examples in Linux – Part 2
In our last article, we covered various examples of the ‘sort‘ command. If you missed it, you can catch up by following the link below. In this post, we will continue from where we left off in the previous article, to cover the remaining aspects of the command. This way, both articles together will serve as a comprehensive guide to the Linux ‘sort‘ command.
Before we proceed further, please create a text file named ‘month.txt‘ and populate it with the data provided below.
echo -e "mar\ndec\noct\nsep\nfeb\naug" > month.txt cat month.txt
15. Sorting File Content by Month
The following command sorts the contents of the file “month.txt” in chronological order based on the month abbreviation or name using the
'-M' option that instructs the ‘sort‘ command to treat the data as dates and sort them accordingly.
sort -M month.txt
16. Sorting Output by File Size in Human-Readable Format
The following command combines the ‘ls‘ and ‘sort‘ commands to perform two tasks. First, it lists the contents of the user’s home directory in long format and then, it pipes this directory listing to the ‘sort’ command to print file sizes in a human-readable format, making it easier to identify the largest and smallest files in the directory.
ls -l /home/$USER | sort -h -k5
17. Checking Sorted Files for Consistency
In the previous article, we generated two text files: ‘sorted.txt‘ in example number 4 and ‘lsl.txt‘ in example number 6. We are aware that ‘sorted.txt‘ is already sorted, whereas ‘lsl.txt‘ is not.
To verify the sorting status of both files, we will employ the ‘sort‘ command, which will allow us to confirm whether ‘sorted.txt‘ remains in the correct order and whether ‘lsl.txt‘ requires sorting.
sort -c sorted.txt
If it returns 0, means that the file is sorted and there is no conflict.
sort -c lsl.txt
18. Handling Delimiters When Not Using Spaces in File
If the delimiter (separator) between words is a space, the ‘sort‘ command automatically interprets anything after a horizontal space as a new word. But what happens when the delimiter is not a space?
Consider a text file, the contents of which are separated by anything other than space such as
Create a text file where contents are separated by
+. Use the cat command to check the contents of the file.
echo -e "21+linux+server+production\n11+debian+RedHat+CentOS\n131+Apache+Mysql+PHP\n7+Shell Scripting+python+perl\n111+postfix+exim+sendmail" > delimiter.txt
$ cat delimiter.txt
Now sort this file based on 1st field which is numerical.
sort -t '+' -nk1 delimiter.txt
The second is based on 4th field which is non-numeric.
If the delimiter is Tab you may use
$'\t' in place of
'+', as shown in the above example.
19. Randomly Sorting Output by File Size
Sort the output of the ls -l command for your home directory based on the fifth column, which represents the ‘amount of data’, in random order.
ls -l /home/avi/ | sort -k5 -R
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 Rule number – 2 from the last article, the sort command prefers lines starting with lowercase characters over uppercase characters. Also check example 3 in the last article, where the string ‘laptop‘ appears before the string ‘LAPTOP‘.
20. Overriding Default Sorting Preferences
How to override the default sorting preference? Before we can override the default sorting preference, we need to export the environment variable
To do this, run the code below in your commandline prompt.
And then sort the text file ‘tecmint.txt‘ overriding the default sort preference.
$ sort tecmint.txt
Don’t forget to compare the output with the one you achieved in example 3 and also you can use the option ‘
-f‘ aka ‘
--ignore-case‘ to get much-organized output.
$ sort -f tecmint.txt
21. Combining Two Input Files in a Single Operation
How about running ‘sort‘ on two input files and joining them in one go?
Let’s create two text files, namely ‘file1.txt‘ and ‘file2.txt‘, and populate them with some data. In ‘file1.txt‘, we’ll add numbers as shown below. We’ll also use the cat command to inspect the file’s content
echo -e “5 Reliable\n2 Fast\n3 Secure\n1 open-source\n4 customizable” > file1.txt cat file1.txt
And populate the second file ‘file2.txt‘ with some data as.
echo -e “3 RedHat\n1 Debian\n5 Ubuntu\n2 Kali\n4 Fedora” > file2.txt cat file2.txt
Now sort and join the output of both the files.
join <(sort -n file1.txt) <(sort file2.txt)
In conclusion, this article has explored various aspects of the ‘sort‘ command in Linux. We began with basic sorting by alphabetical order, ventured into numeric and date-based sorting, and even touched upon custom delimiters. We also learned how to override default sorting preferences to suit our needs.
Moreover, we discussed techniques for checking whether a file is already sorted and combining ‘sort‘ operations for multiple input files. With the knowledge gained here, you have a powerful tool at your disposal for sorting and organizing data efficiently in a Linux environment.
We hope this article has been insightful and useful for your command-line endeavors. Keep exploring, learning, and mastering the art of ‘sort‘ to enhance your Linux skills.