Updated April 18, 2023
Introduction to Linux sort Command
The Linux Sort command is assuming the input contents in ASCII format accordingly it will sort the files and the arranging the records in giving specific order. It is a command-line utility to sort the lines from input files. The sort command is also supporting the reverse order sorting, number sorting, month sorting, alphabetical sorting, etc.
The Linux Sort command-line utility was written by Mike Haertel and Paul Eggert.
Syntax of Tee command
sort [OPTION]... [FILE]...
- sort: Using the Sort keyword in the command | syntax. It will take the two set of arguments as an OPTION and FILE and sort the input data.
- OPTION: We can provide the different flags as an option to sort command.
- FILE: Input data or Input file.
How does Linux Sort Command work?
The Linux Sort command or utility is getting the input data from FILE and sort the output data as per the sorting rules as well as the flag option in the sort command. The sort command sorting the input data line by line.
There are three rules for sorting:
1. The output line starts with a number that will display first as compare to character or letter.
2. The output line starts with an earlier letter alphabet it will display first.
3. The output line will display lowercase letters will display first.
Examples of Linux sort Command
Here are the following examples metioned:
Example #1 – Sort Command
Sort command, it is a very basic command. It will display the default result of sorting (working on above 3 primary rules of sorting)
Command :
cat file.txt
sort file.txt
Explanation :
We are having a sample directory. In the same directory, we have the “file.txt” file in it. In the “f1.txt” file, we are having some data in it (refer screenshot 1 (a)). With the help of sorting command we have applied the sporting mechanism and sort the data as per the sorting rule like number first, upper case second, and the character | letter comes first in the alphabet.
Output :
Screenshot 1 (a)
Screenshot 1 (b)
Example #2 – Sort Command with “-o” option
In the Sort command, there is a built-insort “-o” option functionality. With the help of this functionality, we can able to redirect the sorted output to the file.
Command :
cat file.txt
sort file.txt > file1.txt
sort -o file1.txt file.txt
Explanation :
In the above two commands, the first command will sort the data and pass to the file “file1.txt” (refer to the screenshot 2 (a)). With the help of the built-in option in sort “-o” we are redirecting the sorted output to the different file “file.txt” (refer the screenshot 2 (b)).
Output :
Screenshot 2 (a)
Screenshot 2 (b)
Example #3 – Sort Command with “-r” option
In Sort command, we can get the sorted output in reverse format.
We need to use the “-r” option in the Sort command.
Command :
cat file1.txt
sort -r file1.txt
Explanation :
In the above command, we can able to get the sorted output but in reverse format. In the output, we are using the “-r” option in the sort command.
Output :
Example #4 – Sort Command with “-k” option
In Sort command, we can sort the data on the column level.
We can use the “-k” option and the column number in the Sort command to sort the column level sorting.
Command :
cat col.txt
sort -k2 col.txt
Explanation :
We are having a sample directory. In the same directory, we have the “col.txt” file in it. In the “f1.txt” file, we are having row and column level data (refer screenshot 3 (a)). Now we need to sort the data as per column level. As per the above command, we have used the “-k” option with the column number “2” which need to sort.
We can sort the input data as per the second column (refer screenshot 3 (b)).
Output :
Screenshot 3 (a)
Screenshot 3 (a)
Example #5 – sort command with the “-n” option
In the Linux Sort command, we can sort the input data as per the numeric value.
We need to use the “-n” option in the Sort command.
Command :
cat num.txt
sort -n num.txt
Explanation :
In the “num.txt” file, there is the number of numeric data in it. With the help of the “-n” option, we sort the input numeric data.
Output :
Example #6 – sort command with “-nr” option
In the Linux Sort command, we can sort the input data as per the numeric value but in reverse format.
We need to use the “-nr” option in the Sort command.
Command :
cat num.txt
sort -nr num.txt
Explanation :
In the “num.txt” file, there is the number of numeric data in it. With the help of the “-nr” option, we sort the input numeric data but in reverse format.
Output :
Example #7 – Sort Command with “-M” option
In Linux Sort command, we are having the functionality to sort the input month name in month ascending order.
To get the list of the month in ascending order we need to use the “-M” option with the sort command.
Command :
cat mon.txt
sort -M mon.txt
Explanation :
In the “mon.txt” file, we are having the random month names in it. If we need to sort in a proper month ascending format we can use the “M” option in the sort command. After using this value, we can get the month list in a sorted format.
Output :
Conclusion
We have seen the uncut concept of “Linux Sort Command” with the proper example, explanation, and command with different outputs. The default sort command is working on the three rules but as per the requirement, we can sort the different types of data as well as the row and column data. While sorting the data, the original data will not hamper.
Recommended Articles
We hope that this EDUCBA information on “Linux sort Command” was beneficial to you. You can view EDUCBA’s recommended articles for more information.