sort
Is a command in Linux that sorts files or text contents from standard input. It supports sorting by numerical, dictionary, month, random, etc., and can control the sorting order (ascending or descending order).sort
It is often used to process and organize text files, such as logs, data tables, etc.
grammar
sort [Options]... [document]...
Common options
- -n: Sort by numerical value (applicable to text containing numbers).
- -r: Sort in descending order (default is ascending).
- -k: Specifies the sorted columns (fields), which are sorted by the entire row by default.
- -t: Specify the field separator, which is a whitespace character (space, tab character) by default.
- -u: Remove duplicate rows and output only unique rows.
- -o: Specify the output file and write the sorting result directly to the file, not the standard output.
- -f: Ignore case for sorting.
- -b: Ignore the blank characters at the beginning of the line.
- -M: Sort by month (such as "Jan", "Feb", "Mar").
- -c: Check whether the file has been sorted. If it is not sorted, the diagnostic information will be output.
- -V: Natural order sorting (support version number sorting, such as "file1" before "file10").
Specific use
Sort by alphabetical order
sort
WillThe contents are sorted alphabetically and output to the terminal.
Sort by numerical value
sort -n
For the filenumerical sort of numbers in it.
Arrangement in descending order
sort -r
rightSort in descending order.
Sort by specific columns
Assuming FileThe contents are as follows:
John 30Alice 22Bob 25
If you want to sort by age in the second column:
sort -k 2 -n
Output:
Alice 22Bob 25John 30
Sort by specific separator
AssumptionsThe file contents are as follows (separated by commas):
John,30Alice,22Bob,25
You can specify a comma as a separator and sort it in the second column:
sort -t ',' -k 2 -n
Output:
Alice,22Bob,25John,30
Remove duplicate rows
sort -u
rightThe contents of , sort and remove duplicate rows.
Ignore case sorting
sort -f
rightThe file contents are sorted and letter case is ignored.
Sort by month
Assuming FileThe content is as follows:
MarJanFeb
Sort by month order:
sort -M
Output:
JanFebMar
Check if the files are sorted
sort -c
If the fileIf not arranged alphabetically, an error message will be prompted.
Output the result to a file
sort -o sorted_file.txt
Write the sorted results tosorted_file.txt
in the file.
Comprehensive use
Suppose there is a CSV file, the content is as follows:
John,Developer,30Alice,Manager,22Bob,Tester,25
To sort the results in descending order by age (third column) and write the results intosorted_employees.csv
document:
sort -t ',' -k 3 -n -r -o sorted_employees.csv
Summarize
sort
It is a powerful and flexible text processing tool in Linux, supporting multiple sorting methods and combinations. Depending on your needs, you can combine different options to handle complex sorting tasks.
This is the end of this article about the detailed explanation of the Linux sort command. For more related contents of Linux sort command, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!