Detailed explanation of the sort command for basic introduction to Linux

巴扎黑
Release: 2017-08-17 10:22:58
Original
1832 people have browsed it

sort is a very commonly used command in Linux. Each line of the file is treated as a unit and compared 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.

There is a file test here, the content is:

8723 23423

321324 213432

23 234

123 231

234 1234

654 345234

1. Sort’s -t option and -k option

sort provides the -t option, followed by You can set the 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

If you want to compare the 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

3. The -n option of sort

sort defaults to comparing by ASCII code value, 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

sort -rn test
Copy after login

Output result:

321324 213432

8723 23423

654 345234

234 1234

123 231

23 234

Attachment: Detailed explanation of sort command parameters

-f Convert all lowercase letters to uppercase letters for comparison, that is, Ignore case

-c Check whether the file is 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 file is sorted Already sorted, if out of order, no content will be output, only 1 will be returned

-M Sort by month, such as JAN is less than FEB, etc.

-b Ignore all blanks in front of each line Part, start comparing from the first visible character

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

The above is the detailed content of Detailed explanation of the sort command for basic introduction to 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!