View the sort command instructions for file and content processing.

Source: Internet
Author: User

View the sort command instructions for file and content processing.
1. Command description:

The sort command sorts the text content of a file.

2. Syntax:

Sort [Option]... [file list]...

3. instance:

To demonstrate sort usage, create a file first. The file name and content are as follows:

[root@CentOS opt]# cat sort.txt b:3c:2a:4e:5d:1f:11[root@CentOS opt]# 

Next, sort the output content:

[Root @ CentOS opt] # cat sort.txt | sort ???? # When the output content is directly sorted, a: 4b: 3c: 2d: 1e: 5f: 11 [root @ CentOS opt] is sorted by the first character of each line by default. #

Next, sort the output content in reverse order:

[Root @ CentOS opt] # cat sort.txt | sort-r ???? #-R option indicates reverse sorting f: 11e: 5d: 1c: 2b: 3a: 4 [root @ CentOS opt] #

The sort.txt file has the following characteristics: the first character is a letter, the third character is a number, and the middle is separated by a colon. In this way, you can use the-t option to specify the delimiter and use the-k option to specify the columns used for sorting:

[root@CentOS opt]# cat sort.txt | sort -t ":" -k 2d:1f:11c:2b:3a:4e:5[root@CentOS opt]# 

You may have noticed that the current sorting is performed according to the part of the number column. However, what is 2nd behavior? This is because the current sorting is not performed according to the "number value ". In the preceding command, the current sorting is performed according to the second part separated by colons. By sorting, only the first character is read, and the first character of 11 is 1, sort by characters, which is indeed smaller than 2. If you want to specify the order by number, you need to add the-n option:

[Root @ CentOS opt] # cat sort.txt | sort-t ":"-k 2-n ???? #-N indicates that the values are sorted by numbers d: 1c: 2b: 3a: 4e: 5f: 11 [root @ CentOS opt] #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.