Sort Command in Linux with Examples
SORT command in Linux is used to arrange the record in a specific order according to the option used. It helps in sorting the data in the file line by line. SORT command has different features that it follows in the resultant of commands. First is that the lines having numbers will come before the alphabetic lines. Those lines having lower case letters will be displayed earlier than the lines having the same character in uppercase.
Prerequisite:
You need to install Ubuntu on a virtual box and configure it. Users must be created to have the privileges of accessing the applications.
Syntax:
Example:
This is a simple example of sorting a file having data of names. These names are not in order, and to make them in an order form you need to sort them.
So, consider a file named file1.txt. We will display the contents in the file by using the appended command:
Now use the command to sort the text in the file:
Save the Output in Another File
By using the sort command, you will come to know that its result is only displayed but not saved. To capture the result we need to store it. For this purpose –o option in the sort command is used.
Consider an example name sample1.txt having the names of cars. We want to sort them and save the resultant data in a separate file. A file named result.txt is created at run-time and the respective output is stored in it. The data of sample1.txt is transferred to the resultant file and then with the help of –o the respective data is sorted. We have displayed the data using the cat command:
$ sort –o result.txt sample1.txt
$ Cat result.txt
The output shows that the data is sorted and saved in another file.
Sort for Column Number
Sorting is not only done on a single column. We can sort one column because of the second column. Let us have an example of a text file in which there are names and marks of the students. We want to organize them in ascending order. So we will use the keyword –k in the command. Whereas –n is used for numerical sorting.
As there are two columns, so 2 is used with n.
Check the Sorted Condition of a File
If you are not assured if the present file is sorted or not, remove this doubt using the command that clarifies the confusion and displays the message. We will come through two basic examples:
Unsorted Data
Now, consider an unsorted file having the vegetable names.
The command will use the keyword –c. This will check whether the data in the file is sorted or not. If the data is unsorted, then the output will display the line number of the first word where unsortedness is present in the file and also the word.
From the given output, you can understand that the 3rd word in the file was misplaced.
Sorted Data
In this case, when the data is already organized, there is no need to do anything else. Consider a file result.txt.
From the result, you can see that no message is shown which indicates that the data in the respective file is already sorted.
Remove Duplicate Items
Here is the most useful option of some sort. This helps in removing the repeated words in a file and make the file item organized too. It also maintains the consistency of the data in the file.
Consider the file name file2.txt having the names of subjects but one subject is repeated multiple times. Sort command will then use the –u keyword to remove duplication and relatedness:
Now, you can see that the repeated items are removed from the output and that the data is also sorted.
Sort Using Pipe in a Command
If we want to sort the data of the file by providing the list of the directory concerning the file sizes, we will enlist all respective data of the directory. The ‘ls’ is used in command and -l will display it. The Pipe will help in displaying the files in an organized manner.
$ ls –l /home/aqsayasin/ | sort –nk5
Random Sorting
Sometimes, while performing any function, you can mess with the arrangement. If you want to arrange the data in any sequence and if there are no criteria for sorting, then random sorting is preferred. Consider a file named sample3.txt having the names of the continents.
The respective output shows that the file is sorted and items are arranged in a different order.
Sort the Data of Multiple Files
One of the most useful commands of sorting is to sort the data of different files at a time. This can be done by using the find command. The output of the find command will act as an input for the command after the pipe that is a sort command. Find keyword is used to give only one file on each line, or we can say that it uses a break after each word.
For instance, let’s consider three files named sample1.txt, sample2.txt, and sample3.txt. Here the “?” represents any number that is followed by the word “sample”. Find will fetch all three files and their data will be sorted with the help of a sort command with the pipe initiative:
$ find –name “sample?.txt” –print0 | sort –files0-from=-
The output shows that the data of all sample.txt series files are displayed and is arranged and organized alphabetically.
Sort with Join
Now, we are introducing an example that is quite different from the ones that are discussed earlier in this tutorial. In addition to sort, we have used join. This process is done in such a way that both the files are first sorted and then joined using a join keyword.
Consider two files you want to join.
Now use the below-cited query to apply the given concept:
$ join (sort sample2.txt) (sort sample3.txt)
You can see from the output that the data both files are combined in sorted form.
Compare Files Using Sort
We can also adopt the concept of comparing two files. The technique is the same as it was for joining. Firstly two files are sorted and then the data in them are compared.
Consider the same two files as discussed in the previous example. Sample2.txt and sample3.txt:
$ comm (sort sample2.txt) (sort sample3.txt)
The data is sorted and arranged alternatively. The initial line of the file sample2.txt is written next to the first line of the file sample3.txt.
Conclusion
In this article, we have talked about the basic functionality and options of the sort command. Linux sort command is very beneficial in the maintenance of data and filtering all useless items from the files.