What is the linux sort command?

烟雨青岚
烟雨青岚Original
2020-06-29 15:08:464864browse

The linux sorting command is sort. sort treats each line of the file as a unit and compares them with each other. The comparison principle is to compare from the first character to the last, by ASCII code value, and finally output them in ascending order.

What is the linux sort command?

The linux sorting command is sort.

sort treats each line of the file as a unit and compares them with each other. The comparison principle is to compare from the first character backward, according to the ASCII code value, and finally output them in ascending order.

[zookeeper@master rh]$ cat seq.txt
banana
apple
pear
orange
pear
[zookeeper@master rh]$ sort seq.txt 
apple
banana
orange
pear
pear

The -u option of sort

Its function is very simple, that is, to remove duplicate lines from the output lines.

[zookeeper@master rh]$ sort -u seq.txt 
apple
banana
orange
pear

pear was ruthlessly deleted by the -u option due to duplication.

The -r option of sort

[zookeeper@master rh]$ cat number.txt
1
3
5
7
11
2
4
6
10
8
9
[zookeeper@master rh]$ sort number.txt  --sort默认的排序方式是升序
1
10
11
2
3
4
5
6
7
8
9
[zookeeper@master rh]$ sort -n number.txt      --排序程序将这些数字按字符来排序了,排序程序会先比较1和2,显然1小,所以就将10放在2前面
1
2
3
4
5
6
7
8
9
10
11
[zookeeper@master rh]$ sort -n -r number.txt      --r表示降序,n表示按数字进行排序
11
10
9
8
7
6
5
4
3
2
1

Other common sort options

-f will convert all lowercase letters to Uppercase letters are used for comparison, that is, case is ignored.

-c will check whether the file is sorted. If it is out of order, it will output the relevant information of the first out-of-order line, and finally return 1

-C will check whether the files are sorted. If they are out of order, the content will not be output and only 1 will be returned.

-M will be sorted by month, such as JAN is smaller than FEB, etc.

-b will ignore all white space in front of each line and start the comparison from the first visible character.

Recommended tutorial: "linux tutorial"

The above is the detailed content of What is the linux sort command?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn