Linux sort function

Source: Internet
Author: User

The sort command is very useful in Linux, which sorts the files and outputs the sort results standard. The sort command can get input from either a specific file or from stdin.

From: Http://man.linuxde.net/sort

-------------------------------------------------

sort [parameters] [parameter] File

Options:

-B: Ignores the space character that starts at the beginning of each line;-C: Checks if the file is sorted in order;-D: When sorting, the letters, numbers, and whitespace characters are processed, and the other characters are ignored;-f: When sorting, lowercase letters are treated as uppercase letters;-I: When sorting, In addition to ASCII characters between 040 and 176, omit other characters;-m: Merge the files of several sort numbers;-M: Sort the first 3 letters according to the abbreviation of the month;-N: Sort the;-o< output file according to the size of the value;: Save the sorted result in the prepared file ;-r: Sort in reverse order;-t< delimited character;: Specifies the field separator character to use when sorting;

-U: Ignore same row usage (uniq file = Sort-u file)

-R: Random sort
+< start >-< end fields: Sort by the specified fields, ranging from the Start field to the previous column in the End field.

Usage of K:

Fstart.cstart modifie,fend.cend Modifier       -------Start--------,-------End--------    fstart.cstart option, Fend.cend options This syntax format can be separated into two parts, the start part and the end part. The start section is also made up of three parts, the modifier part of which is what we said earlier about the options section like N and R. We focus on the Fstart and C.start of the Start section. C.start can also be omitted, and the omitted words are indicated from the beginning of the domain. Fstart.cstart, where Fstart is the field that represents the use, and Cstart means "sort first character" from the first character in the Fstart field. Similarly, in the end section, you can set fend.cend, if you omit. Cend, the end to "domain Footer", which is the last character of the domain. Or, if you set Cend to 0 (0), it is also the end to "domain Footer".

Example:

sort only for the second letter of the company's English name, if the same is sorted by employee salary in descending order: $Sort-T' '-K1.2,1.2-nrk3,3facebook.txt Baidu -  theGoogle the  theSohu - 4500Guge -  thebecause only the second letter was sorted, we used the-K1.2,1The representation of. 2 means that we "only" sort the second letter. (If you ask "I use-K1.2 Why not? ", of course not, because you omit the end part, which means you will sort the string from the second letter to the last character in the field. We also use the-K to sort employees ' wages3,3, this is the most accurate expression, which means that we "only" sort the domain, because if you omit the following 3, it becomes "we sort the contents of the beginning of the 3rd field to the last domain location".

Now let's give a few simple examples:

#将BB列按照数字从小到大顺序排列: [[email protected] text]# sort-nk 2-t: sort.txt AAA:BB:CC bbb:10:2.5 ddd:20:4.2 aaa:30:1.6 eee:40:5.4 ccc:50:3.3 eee:60:5.1 #将CC列数字从大到小顺序排列: [[email protected] text]# sort-nrk 3-t: sort.txt eee:40:5.4 eee:60:5.1 Ddd:20:4 .2 ccc:50:3.3 bbb:10:2.5 aaa:30:1.6 AAA:BB:CC #-N is sorted by number size,-r is in reverse order,-K is the field that specifies the sort that needs to be sorted,-t specifies that the field delimiter is a colon

  

Linux sort function

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.