Sort is a command-line utility in Linux that helps you to sort the data in a file line by line. It has a lot of options that allow you to arrange the record in a specific order. It supports sorting, in reverse order, by month, by number, alphabetically, and more. The sort command only prints the sorted output; it doesn’t actually sort the files.
In this post, we will show you how to use the sort command with examples in Linux.
1. Sort a File Alphabetically
By default, the sort command will sort lines alphabetically.
For the purpose of this tutorial, create a new file named file.txt:
nano file.txt
Add the following lines:
baroda ahmedabad Baroda rajkot jamnager junagadh Surat Gandhinagar
Save the file, then use the sort command to sort the file alphabetically:
sort file.txt
Sample output:
ahmedabad baroda Baroda Gandhinagar jamnager junagadh rajkot Surat
The sort command only sorts the output of the file. If you want to save the sorted file, then run the following command:
sort file.txt > sortedfile.txt
2. Sort a File in Reverse Order
You can use the -r flag with the sort command to sort the file in reverse order:
sort -r file.txt
Sample output:
Surat rajkot junagadh jamnager Gandhinagar Baroda baroda ahmedabad
3. Sort a File by Number
You can use the -n flag to sort the file from lowest number to highest number.
First, let’s create a sample file:
nano file.txt
Add the following lines:
1. Hitesh 4. Jayesh 2. Vyom 7. Disha 3. Ramesh 9. Vrat
Save and close the file, then use the sort command to sort a file by number:
sort -n file.txt
Sample output:
1. Hitesh 2. Vyom 3. Ramesh 4. Jayesh 7. Disha 9. Vrat
4. Sort a Mixed-case File
If a file contains lowercase and uppercase content, then the sort command will sort uppercase first. In this case, you can use the -f option to ignore the case.
Let’s create a sample file:
nano file.txt
Add the following content:
Haresh hamir Hari heta himani
Now, use the sort command with -f option:
sort -f file.txt
Sample output:
hamir Haresh Hari heta himani
5. Check If a File is Sorted
You can use the -c option to check if a file is sorted or not.
sort -c file.txt
Sample output:
sort: file.txt:2: disorder: hamir
If there is no output, then the file is sorted.
6. Sort a File and Remove Duplicates
You can also use the -u option to find and remove the duplicate lines from the sorted output.
Let’s create a sample file:
nano file.txt
Add the following contents:
Hitesh Jayesh Jayesh Hitesh Samir Ram
Now, use the sort command to remove the duplicate lines from the sorted output:
sort -u file.txt
Sample output:
Hitesh Jayesh Ram Samir
7. Sort a File by Month
You can pass the -M option with the sort command to sort a file by month name.
Let’s create a sample file:
nano file.txt
Add the following lines:
january march june february september december
Save and close the file then sort a file by month using the following command:
sort -M file.txt
Sample output:
january february march june september december
8. Sort a File by Column
If you have a file with multiple columns and want to sort any specific column, then you can use the -k option.
Let’s create a sample file to test.
nano file.txt
Add the following lines:
2. anand 4. ear 3. bharat 1. janak 5. canal
Save and close the file, then sort the second column using the following command:
sort -k2 file.txt
Sample output:
2. anand 3. bharat 5. canal 4. ear 1. janak
To sort the first column, run:
sort -k1 file.txt
Sample output:
1. janak 2. anand 3. bharat 4. ear 5. canal
Conclusion
In this guide, we explained how to sort a file with the sort command in Linux. You can now experiment with the sort command with other options and check the result. You can start using the command now on your VPS from Altantic.Net.