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.
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>
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>
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.
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>
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.
Comments