Home > Operation and Maintenance > Linux Operation and Maintenance > Detailed explanation of the use of sort command in Linux

Detailed explanation of the use of sort command in Linux

黄舟
Release: 2017-06-07 10:13:57
Original
1773 people have browsed it

sort is a very commonly used command in Linux. It treats each line of the file as a unit and compares it with each other. The comparison principle is from the first character backward and press ASCII in sequence. The code values ​​are compared, and finally they are output in ascending order.

There is a file test here, the content is:

8723 23423
321324 213432
23 234
123 231
234 1234
654 345234
Copy after login

1. The -t option and -k option of sort

sort provides the -t option, which can be set later separator, -k to specify the number of columns.

Sort the first column

sort test
Copy after login

Sort the second column

sort -k 2 test
Copy after login

If you change the content of the test file to:

8723,23423
321324,213432
23,234
123,231
234,1234
654,345234
Copy after login

If you want to The second column is sorted by size

sort -t "," -k 2 test
Copy after login

If there is no -t option, it is the default space or tab key, so the -t option is not used above.

2. Use the -r option to sort in reverse order

The default sorting method of sort is ascending order, and the -r parameter is changed to descending order

sort -r test
Copy after login

Output result:

8723 23423
654 345234
321324 213432
234 1234
23 234
123 231
Copy after login

3. The -n option of sort

sort compares by ASCII code value by default, so if you look at the results in 2 above, you will find that 8723 is ranked first compared to 321324. So how do we make it sort by numerical size? This is when the -n parameter comes into play.

sort -n test
Copy after login

Output result:

23 234
123 231
234 1234
654 345234
8723 23423
321324 213432
Copy after login
sort -rn test
Copy after login

Output result:

321324 213432
8723 23423
654 345234
234 1234
123 231
23 234
Copy after login

Attachment: Detailed explanation of sort command parameters

-f Convert all lowercase letters to uppercase letters. Compare, that is, ignore case

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

-C 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 Sort by month, such as JAN is less than FEB, etc.

-b Ignore each All blank parts in front of a line are compared starting from the first visible character

-u Remove duplicate lines from the output line without changing the content of the file itself

The above is the detailed content of Detailed explanation of the use of sort command in Linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template