7 Interesting Linux ‘sort’ Command Examples – 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 "marndecnoctnsepnfebnaug" > month.txt
cat month.txt

Populate Content

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

Sort File Content by Month in Linux

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

Sort Content Human Readable Format

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

Check File is Sorted

If it returns 0, means that the file is sorted and there is no conflict.

sort -c lsl.txt

Check File Sorted Status

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 '|' or '' or '+' or '.' or ...</code.

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+productionn11+debian+RedHat+CentOSn131+Apache+Mysql+PHPn7+Shell Scripting+python+perln111+postfix+exim+sendmail" > delimiter.txt
$ cat delimiter.txt

Check File Content by Delimiter

Now sort this file based on 1st field which is numerical.

sort -t '+' -nk1 delimiter.txt

Sort File By Fields

The second is based on 4th field which is non-numeric.

Sort Content By 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 

Sort Content by Column in Random Order

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 'LC_ALL' to 'C'.

To do this, run the code below in your commandline prompt.

export LC_ALL=C

And then sort the text file ‘tecmint.txt‘ overriding the default sort preference.

$ sort tecmint.txt

Override Sorting Preferences

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

Compare Sorting Preferences

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 Reliablen2 Fastn3 Securen1 open-sourcen4 customizable” > file1.txt
cat file1.txt

Populate Content with Numbers

And populate the second file ‘file2.txt‘ with some data as.

echo -e “3 RedHatn1 Debiann5 Ubuntun2 Kalin4 Fedora” > file2.txt
cat file2.txt

Populate File with Data

Now sort and join the output of both the files.

join <(sort -n file1.txt) <(sort file2.txt)

Sort Join Two Files

Conclusion

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.

Similar Posts