The Sort command in Unix allows us to sort or arrange the contents of a system file, arrange data in a sorted fashion, and redirect output to stdout. The sort command, by default, aids in sorting files with ASCII-formatted information. We may also sort the items numerically or in various ways by using the arguments provided by the sort command. To learn more about UNIX development, join the UNIX Training in Chennai at FITA Academy, which will provide you with a diverse skill set and the best Placement Training.
In this blog, I’ll explain What is the UNIX Sort by Column Commands.
Features of Sort Command
The sort command in Unix is a handy and often used command in shell scripting to filter input and display the output to stdout.
- The sort command in Unix helps in arranging or sorting the contents in a file line by line.
- The sort command is a command-line utility that actively sorts lines in a text file. This command sorts the contents of the files alphabetically, by month, by number, or in reverse order, and it also removes duplicates.
- By default, a blank space is used as a field separator.
- Sorting in Unix actively requires extracting one or more keys from the input data.
How does Unix Sort by Column do?
The basic syntax of the Sort by command is as follows:
sort [options] [files]
Sort commonly use the following options:
Sort -b: It ignores the blank spaces at the initial stage of the data in the file.
sort -r: It reverses file contents and sorts the order.
sort -o: It specifies and sorts the output file before printing it.
sort -n: It is used to arrange numerical values ascendingly.
sort -M: It is helpful to organise the contents by calendar month.
sort -u: It aids in removing duplicates from the file and returning the output in a sorted manner.
Enroll in Learn UNIX Online, Which will provide more Concepts about UNIX Shell Commands.
Options with Sort Command
Sort commands with options can be applied in a variety of ways. A few sort possibilities are shown below with examples:
When combined with option ‘o,’ Sort allows users to sort the data into a new file and redirect the output in a sorted format. Option ‘o’ aids in redirecting the data to the output file in sorted order.
The sort command in Unix with the ‘r’ argument allows you to sort the contents in reverse order. This option reverses the specified input in descending order by default.
sort -r file_name.sh
To sort a file numerically on Unix, use the ‘-n’ option with the sort command. This command is used to sort the file’s numerical data. It sorts in ascending order by default.
sort -r file_name.sh
This sort option allows you to sort the file’s numerical contents in reverse order. By default, numeric data in reverse order produces descending order of the items.
sort -nr file_name.txt
The -k option allows us to sort a table by column number. To sort a column, use ‘-k’ followed by the column number.
sort -k file_name.txt
The ‘-c’ option checks to see if the contents of the font files are sorted. If the output of this option is not sorted, it will give us the result. If there is no output, the contents of the files have already been sorted. It will also tell us which lines of information need to be sorted.
sort -c file_name.txt
When combined with Sort, this option aids in eliminating duplicates and sorting the file’s contents. By default, while writing to a file, the output removes duplicates and organises the data in ascending order.
sort -u file_name.txt
We can also sort the months in the file using Unix. The output will actively display the months in sorted order.
sort -M file_name.txt
Hopefully, you enjoyed this blog and now understand everything about UNIX, including What is the UNIX Sort by Column Commands.
You can gain more expertise and ability in designing dynamic and interactive web applications by studying at the UNIX Certification in Chennai at FITA Academy.