Detailed explanation of sort command in Linux system

Linux systems provide sortcommands to sort text files. sortCommands can be sorted by row, field or number, and support various sorting options to meet different sorting needs.

Basic syntax and options

sortThe 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.txtsorting 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

sortThe command sorts each line lexicographically by default. For example, suppose you have a file.txttext 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). sortCommands can be sorted by specified fields.

Suppose there is a data.txttext 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 -kthe option to specify the field to sort by, and -tthe 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, sortthe command sorts numbers lexicographically. If you need to sort by numerical value, you can use -nthe option.

Suppose there is a numbers.txttext 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, sortcommands are sorted in ascending order (smallest to largest). If you need to sort in descending order (biggest to smallest), you can use -rthe option.

Continuing with numbers.txtthe 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

sortThe command can also be used to remove duplicate lines in a text file, keeping only the first occurrence. This is achieved using -uthe option.

Suppose there is a names.txttext 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

sortThe command supports sorting by multiple fields. Several -koptions can be used to specify the fields to sort on and the sort order. For example, suppose you have a data.txttext 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 3rspecifies to sort in descending order by the third field ( rindicates reverse order), and then uses -k 2nto specify to sort in ascending order by the second field ( nindicates 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 -bthe option. For example, suppose you have a file.txttext 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

sortCommand 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.txtand 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, sortcommands 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 sortthe command. For example:

echo -e "3\n1\n2" | sort -n

The result of the operation is as follows:

1
2
3

Here echothe command is used to generate a set of numbers and |piped to sortthe command for sorting.

Guess you like

Origin blog.csdn.net/u012581020/article/details/132324749