Linux systems provide sort
commands to sort text files. sort
Commands can be sorted by row, field or number, and support various sorting options to meet different sorting needs.
Basic syntax and options
sort
The basic syntax of the command is as follows:
sort [选项] [文件]
Common options include:
-r
: Sort in reverse order (descending).-n
: Sort by value.-k 字段
: Sort by the specified field. This parameter can specify multiple fields, separated by commas. The default field separator is tab or space.-t 分隔符
: Specifies the delimiter for the field.-u
: Remove duplicate lines and keep only the first occurrence.-f
: Sort regardless of case.-b
: Ignore whitespace characters at the beginning of the line for sorting.-c
: Check whether the file has been sorted, if not, output the first line that does not match the sorted order.-f
: Sort regardless of case. For example,sort -f file.txt
sorting ignores the case of the first letter of a row.-b
: Ignore whitespace characters at the beginning of the line for sorting.-c
: Check whether the file has been sorted, if not, output the first line that does not match the sorted order.
sort by row
sort
The command sorts each line lexicographically by default. For example, suppose you have a file.txt
text file named with the following contents:
apple
banana
cherry
To sort the files alphabetically, you can run the following command:
sort file.txt
The result of the operation is as follows:
apple
banana
cherry
sort by field
Typically, each line of a text file contains multiple fields separated by delimiters (such as tabs or spaces). sort
Commands can be sorted by specified fields.
Suppose there is a data.txt
text file named with the following contents:
Tom 25 Male
Jerry 22 Female
Alice 27 Female
To sort by the second field (age), you can use -k
the option to specify the field to sort by, and -t
the separator for the field (here, a space) with the option:
sort -k 2 -t ' ' data.txt
The result of the operation is as follows:
Jerry 22 Female
Tom 25 Male
Alice 27 Female
numerical order
By default, sort
the command sorts numbers lexicographically. If you need to sort by numerical value, you can use -n
the option.
Suppose there is a numbers.txt
text file named with the following contents:
10
2
100
50
To sort numerically, you can run the following command:
sort -n numbers.txt
The result of the operation is as follows:
2
10
50
100
reverse order
By default, sort
commands are sorted in ascending order (smallest to largest). If you need to sort in descending order (biggest to smallest), you can use -r
the option.
Continuing with numbers.txt
the file as an example, to sort in reverse order, you can run the following command:
sort -n -r numbers.txt
The result of the operation is as follows:
100
50
10
2
remove duplicate rows
sort
The command can also be used to remove duplicate lines in a text file, keeping only the first occurrence. This is achieved using -u
the option.
Suppose there is a names.txt
text file named with the following contents:
Tom
Jerry
Tom
Alice
Jerry
To remove duplicate lines, you can run the following command:
sort -u names.txt
The result of the operation is as follows:
Alice
Jerry
Tom
sort by multiple fields
sort
The command supports sorting by multiple fields. Several -k
options can be used to specify the fields to sort on and the sort order. For example, suppose you have a data.txt
text file named with the following contents:
Tom 25 Male
Jerry 22 Female
Alice 27 Female
To sort by gender (third field) in descending order and then age (second field) in ascending order, you can run the following command:
sort -k 3r -k 2n data.txt
The result of the operation is as follows:
Alice 27 Female
Jerry 22 Female
Tom 25 Male
Here use -k 3r
specifies to sort in descending order by the third field ( r
indicates reverse order), and then uses -k 2n
to specify to sort in ascending order by the second field ( n
indicates to sort by value).
ignore leading characters
Sometimes each line of a text file may contain some leading characters, such as spaces or tabs. If you wish to ignore these leading characters when sorting, you can use -b
the option. For example, suppose you have a file.txt
text file named with the following contents:
apple
banana
cherry
To ignore leading spaces and tabs when sorting, you can run the following command:
sort -b file.txt
The result of the operation is as follows:
apple
banana
cherry
Merge multiple files sorted
sort
Command can also merge and sort multiple files. Just list the file names to be sorted one by one. For example, suppose there are two files file1.txt
and file2.txt
, the contents are as follows:
=== file1.txt ===
apple
cherry
=== file2.txt ===
banana
orange
To combine two files and sort them lexicographically, you can run the following command:
sort file1.txt file2.txt
The result of the operation is as follows:
apple
banana
cherry
orange
The result of the merge is to sort the contents of both files as a whole.
Sort data read from standard input
In addition to reading data from files, sort
commands can also read data from standard input and sort it. This can be conveniently combined with other commands. For example, to sort a set of numbers, you can pipe it to sort
the command. For example:
echo -e "3\n1\n2" | sort -n
The result of the operation is as follows:
1
2
3
Here echo
the command is used to generate a set of numbers and |
piped to sort
the command for sorting.