Sort by Column in Bash
Once in a while, we all encounter a situation where we have a huge text file and want to sort the data accordingly.
Especially in times when you are given data in tabular form.
Sure, if you have access to GUI, there are multiple tools that do sorting effortlessly for you but what about sorting data by columns in bash directly?
And in this guide, I will walk you through how you can sort data by column in bash.
How to sort by column in bash
Throughout this guide, I will be using the sort command to sort data by column.
To sort columns, you will have to use a -k
flag followed by the number of a column you want to sort.
So the syntax to sort columns using the sort command will look like this:
sort -k [Column_number] Filename
But before going to the sorting examples, let me share how the sort command will sort the data by default:
- Sorts data in alphabetical order
- Numbers will always come before the alphabet
- Lowercase letters are prioritized before the capital ones
In conclusion, It looks like this: Numbers>Lowercase>Uppercase
.
And for the sake of this tutorial, I will be using a file named Students.txt
which contains the basic data of 7 students as follows:
Name Enrollment_No City
Sagar 181240116054 Nadiad
Milan 181240116019 Aurangabad
Anurag 181240116018 Ahmedabad
Priya 181240116001 Ananad
Abhiman 181240116050 Varanasi
Aayush 181240115019 Karnataka
Ankush 181240116056 Bhubaneswar
Let's say I want to sort the 2nd column in the Student.txt
file so I will be using the following:
sort -k 2 Students.txt
And as you can see, the 2nd column is sorted in ascending order (the default behavior).
But what if you want to change how the data is sorted?
Sort columns by numbers in bash
You might be wondering if the numbers are sorted by default then why would I'm even writing this?
Well, the problem is the sort command by default will sort numbers from the leading characters only.
Let me share an example here. Here, I created a sample file with random numbers in one column and used the sort command without any additional options:
And as you can see, the result is messed up!
To solve this issue, you will need to use the -n
flag with the existing command:
sort -n -k [Column_number] Filename
Sort by columns in reverse in bash
You may encounter times when you want to sort columns in descending order.
For that purpose, you will have to utilize the -r
flag:
sort -r -k [Column_number] Filename
Here, I sorted the 3rd column in reverse:
sort -r -k 3 Students.txt
And here you have it!
Sort command can do a lot more...
Sort command when used with multiple flags can do wonders for you, especially when you have multiple files to deal with.
For this purpose, we have a dedicated guide on you can use the sort command with multiple examples:
I hope this guide has solved the queries you had before.
And if you still have doubts, feel free to ask in the comments.