Introduction to Sort Command in Unix
The SORT command in Unix helps us to sort or arrange the contents in a file in the system, arrange the data in a sorted manner, and redirect the output in a stdout format. The sort command by default helps to sort the file having the contents present in an ASCII manner. By using the options with the sort command, we can also sort the contents in a numeric way or in many other ways.
Following are the features of the sort command:
SORT command in Unix helps to arrange or sort the data of a file by line-by-line. The sort command is used to sort lines of files which are text and acts as a command line utility. This command helps in sorting the contents in an alphabetical or by month or by number or in reverse order and also helps in removing duplicates from the files.
Blank space is taken as the field separator by default. Sorting in Unix is generally done based on one or more keys which is extracted for the contents in the input.
What Can We Do From Sort Command?
Below are some of the list of options that are generally used by sort are listed below:
- sort -b: It ignores the blank spaces at the start of the data in the file.
- sort -r: It reverses the contents of files and sorts the order.
- sort -o: It specifies and prints the output file in a sorted way.
- sort -n: It is used to sort the numerical value in ascending order.
- sort -M: It helps to sort the contents as per the calendar month.
- sort -u: It helps to remove the duplicates in the file and return the output in a sorted way.
Sort command in Unix is written with options that are explained above and the file for which we would need to operate the sort option. The basic syntax of the sort command is given below:
sort [options] [files]
Examples of Using Sort Command in Unix
Let us suppose we have created a filename called testing.sh and have written few contents in it as below:
We can list the contents in the file by using ‘cat’ command as shown below:
Now with the help of the sort command, we will sort the data in the file.
Here we can see that the files have been sort in alphabetical order. By using sort command.
If we have data in the files both in upper and lower case, by default, the sort command will sort the data first in upper case and then to lower case as shown below:
All the upper case data is sorted first and then lower case data is followed.
Options with Sort Command
Sort command with options can be used in several ways. A few of the options for sort are listed below with examples:
1. Option -o
Sort when used with the option ‘o’ helps you to sort the contents into a new file and redirects the output in a sorted form. With the help of option ‘o’, it helps in redirecting the contents to the output file in a sorted form. An example is shown below:
sort testing.sh > outputsh
sort -o output.sh testing.sh
2. Option -r
In Unix, sort command with ‘r’ option gives you to sort the contents in reverse order. This option sorts the given input in a reverse way which is by default in descending order.
sort -r file_name.sh
sort -r testing.sh
3. Option -n
In Unix, when you try to sort a file in a numeric way, you can use the option ‘-n’ with the sort command. This command is used to sort the numeric contents present in the file. Be default, it sorts in ascending order.
sort -r file_name.sh
sort -n number.txt
4. Option -nr
This option in sort helps to sort the numeric contents in the file in reverse order. By default, the numeric data in reverse order gives the contents in descending order.
sort -nr file_name.txt
sort -nr number.txt
5. Option -k
The option -k, gives us the option to sort a table on the order of column number. You can give the column number with ‘-k’ to sort the particular column.
sort -k file_name.txt
sort -k2 file1.txt
6. Option -c
The option ‘-c’ helps to check if the contents in the files are sorted or not. The output of this option will give us the result if it is not sorted. If there is no output, that means the contents in the files are already sorted. It will also give us the line number where the content is not sorted.
sort -c file_name.txt
sort -c testing.sh
7. Option -u
This option when used with sort, helps to remove duplicates and sort the contents in the file. The output produced in the file will give no duplicates and sorted in ascending order by default.
sort -u file_name.txt
sort -u fruits.txt
8. Option -M
Unix also provides us the feature to sort the months in the file. The output that will be given will be the months in a sorted form.
sort -M file_name.txt
sort -M month_list.txt
Sort command in Unix is a very useful and most used command in shell scripting to filter the input contents and to print the output to stdout format. Sort command also helps to sort the contents in alphabetical order, numeric way, reverse order, month wise order, or reverse order. With the combination of the above-mentioned options, you can use the sort command in multiple ways as required by the output. It is to be noted that blank space is considered to be a field separator.
This is a guide to Sort Command in Unix. Here we also discuss the definition and what can we do from the sort command along with different examples and its code implementation. You may also have a look at the following articles to learn more –