Updated May 30, 2023
Introduction to Unix Sort by Column
The following article provides an outline for Unix Sort by Column. 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. Using the options with the sort command, we can also sort the contents numerically or in many other ways.
Features of Sort Command
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.
Below are the few features that are listed to know more about the Sort command
- Sort command in Unix helps to arrange or sort the data of a file line-by-line.
- The sort command actively sorts lines in a text file and functions as a command-line utility. This command helps sort the contents alphabetically, by month, by number, or in reverse order, and helps remove duplicates from the files.
- Blank space is taken as the field separator by default.
- In Unix, sorting actively involves the extraction of one or more keys from the input contents.
How does Unix Sort by Column do?
The basic syntax of sort by command is given below:
sort [options] [files]
Below are some of the list of options that are generally used by sort:
- 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 per the calendar month.
- sort -u: It helps to remove the duplicates in the file and return the output in a sorted way.
Examples of Using Sort Command in Unix
Let us suppose we have created a filename called testing. sh and have written a few contents in it as below:
We can list the contents in the file by using the ‘cat’ command as shown below:
Code:
test% cat testing.sh
Output:
Now, with the sort command’s help, we will sort the data in the file.
Syntax:
sort file_name.sh
Example #1
Here we see that the files have been sorted alphabetically using the sort command.
Code:
test% sort testing.sh
Output:
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.
Example #2
All the upper case data is sorted first, then lower case data is followed.
Code:
test% sort testing.sh
Output:
Options with Sort Command
Sort commands 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 option ‘o’, helps you sort the contents into a new file and redirect the output in a sorted form. With the help of option ‘o’, it helps redirect the contents to the output file in a sorted form.
Example:
Code:
test% sort testing.sh > output.sh
test% sort -o output.sh testing.sh
test% cat testing.sh
Output:
2. Option -r
In Unix, the sort command with the ‘r’ option lets you sort the contents in reverse order. This option reverses the given input, which is by default in descending order.
Syntax:
sort -r file_name.sh
Example:
Code:
test% sort -r testing.sh
Output:
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. By default, it sorts in ascending order.
Syntax:
sort -r file_name.sh
Example:
Code:
test% cat number.txt
Output:
4. Option -nr
This sort option helps to sort the numeric contents in the file in reverse order. By default, the numeric data in reverse order gives the contents descending order.
Syntax:
sort -nr file_name.txt
Example:
Code:
test% sort -nr number.txt
Output:
5. Option -k
Option -k allows us to sort a table in the order of column number. You can give the column number with ‘-k’ to sort the particular column.
Syntax:
sort -k file_name.txt
Example:
Code:
test% cat file1.txt
test% sort -k2 file1.txt
Output:
6. Option -c
The option’-c’ helps to check if the contfiles’ contents are sorted 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.
Syntax:
sort -c file_name.txt
Example:
Code:
test% sort -c testing.sh
test% cat testing.sh
Output:
7. Option -u
When used with sort, this option helps to remove duplicates and sort the contents in the file. When writing to the file, the output actively eliminates duplicates and sorts the data in ascending order by default.
Syntax:
sort -u file_name.txt
Example:
Code:
test% cat fruits.txt
test% sort -u fruits.txt
Output:
8. Option -M
Unix also allows us to sort the months in the file. The output will actively present the months in a sorted form.
Syntax:
sort -M file_name.txt
Example:
Code:
test% cat month_list.txt
test% sort -M month_list.txt
Output:
Conclusion
From the above examples, is it now clear how to use the sort command in Unix Shell Scripting? Modify the option as per the required output, and it will make your filter query easy. 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 alphabetically, numerically, in reverse order, month-wise, 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. Please note that in AWK, a blank space actively serves as a field separator.
Recommended Articles
We hope that this EDUCBA information on “Unix Sort by Column” was beneficial to you. You can view EDUCBA’s recommended articles for more information.