How to Sort in Linux by Multiple Columns

Last updated: January 10, 2023

The sort command is a very powerful tool for sorting and manipulating data in Linux. It provides a lot of options that allow you to customize the way it sorts your data.

One of the useful options of the sort command is the -k, which allows users to specify which columns to sort by. In this tutorial, we learn how to sort by multiple columns in Linux.

Sorting data by specific columns

First, let's check how to use -k option to sort specific columns in a file. For this, we use a sample file with columns.

The following file contains the first column with names and the second column with ages.

sample file containing two columns of data

To sort the data by the first column, use the -k1,1 option. This means using the first field of the file.

sort -k1,1 file.txt <filename>
sort by first column

The output of the command has sorted the data by the first column in alphabetical order.

This data can also be sorted using the second column. For this purpose, we will use the -k2,2 option. This means using the fields from #2 to #2 – only the second field.

sort -k2,2 file.txt <filename>
sort by only second column

The output shows the data in the file.txt has been sorted from the lowest to the highest based on the second column.

I would recommend adding debug (-d) option can be useful for understanding how "sort" is sorting your data and for debugging any issues you may be having with the sorting process. You get a trace of the sorting process.

Sorting by multiple columns

Now, let's check how to sort a file based on multiple columns, not just a single column. We can make use of the same -k option for this purpose.

For example, let's consider a file containing three columns: first name, age, and last name. The sort command can be used to rearrange the rows of the file based on the values in these columns.

sample file containing multiple columns of data

The first column in the file contains identical data, and some values in the second column are also the same. To sort the data based on the values in the first and second columns, use the sort command with the -b, -k1,1, and -k2,2r options.

The -b option is used to ignore leading blanks when sorting the file. The -k1,1 option specifies that the first column should be used as the primary key for sorting the data. The -k2,2r option specifies that the second column should be used as the secondary key and that the data should be sorted in reverse order (from highest to lowest).

sort -b -k1,1 -k2,2r <filename>
 sort a file by the first and second columns

Textfile.txt has been sorted using the first and second columns, as mentioned earlier.

Note: You can also use the "-n" option to specify that the sort should be performed numerically.

Conclusion

By reading this tutorial, you should now proficiently use the Linux sort command to sort data by multiple columns. We hope that you found the information in this post helpful.

Please let us know in the comments section if you have any further questions or want to see additional options covered. We would love to hear your feedback and suggestions.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin started his career in IT in the year 2000 as a computer science instructor and worked as a Linux system engineer and senior analyst roles. Currently working in DevOps environments to increase efficiency and improve delivery time in AWS Cloud infrastructure. He is certified in RHEL, CCNA, and MCP and holds a Masters's in computer science. When in his free time love playing cricket, blogging, and listening to music.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment