Sort Commandis very useful in Linux, it will sort the files and output the sorting results standard. The sort command can get input from either a specific file or from stdin.
Grammar
Sort (options) (parameters)
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: Merges the files of several sort numbers;-m: Sorts the first 3 letters according to the abbreviation of the month;-N: Sort the;-o< output file according to the size of the value;: The sorted result is saved in the specified file ;-U: Remove duplicate rows-r: Sort in reverse order;-K: Specifies which field to sort by-t< separator character;: Specifies the field separator character to use when sorting;+< start >-< end field;: Sort by the specified field, The range is from the Start field to the previous column in the End field.
Parameters
File: Specifies the list of files to be sorted.
Instance
Sort compares each line of a file/text as a unit, comparing it from the first character backwards, to the ASCII value in turn, and finally outputting them in ascending order.
[email protected] text]# cat Sort.txtaaa:10:1.1ccc:30:3.3ddd:40:4.4bbb:20:2.2eee:50:5.5eee:50:5.5[[email protected ] text]# Sort sort.txtaaa:10:1.1bbb:20:2.2ccc:30:3.3ddd:40:4.4eee:50:5.5eee:50:5.5
Ignore the same row using the-u option or uniq:
[[email protected] text]# cat sort.txtaaa:10:1.1ccc:30:3.3ddd:40:4.4bbb:20:2.2eee : 50:5.5EEE:50:5.5[[EMAIL&NBSP;PROTECTED]&NBSP;TEXT]#&NBSP;SORT&NBSP;-U&NBSP;SORT.TXTAAA:10:1.1BBB:20:2.2CCC : 30:3.3ddd:40:4.4eee:50:5.5 or [[EMAIL&NBSP;PROTECTED]&NBSP;TEXT]#&NBSP;UNIQ&NBSP;SORT.TXTAAA:10:1.1CCC : 30:3.3ddd:40:4.4bbb:20:2.2eee:50:5.5
sort:
[[email protected] text]# cat sort.txtaaa:bb:ccaaa:30:1.6ccc:50:3.3ddd:20:4.2bbb : 10:2.5eee:40:5.4eee:60:5.1# the BB column in the Order of numbers from small to large: [[email protected] text]# sort -nk 2 -t: sort.txtaaa:bb:ccbbb:10:2.5ddd:20:4.2aaa:30:1.6eee:40:5.4ccc:50:3.3eee:60:5.1# the CC column numbers from a large to a small order: [[ Email protected] text]# sort -nrk 3 -t: sort.txteee:40:5.4eee:60:5.1ddd : 20:4.2ccc:50:3.3bbb:10:2.5aaa:30:1.6aaa:bb:cc# -n is sorted by numeric size,-R is in reverse order,-K is the field that specifies the sort of love you want,-t specifies that the field delimiter is a colon
The specific syntax format for the-K option:
The syntax format for the-K option:
Fstart.cstart modifie,fend.cend Modifier-------Start--------,-------End--------fstart.cstart options, fend.cend options
This syntax format can be a comma in which,
is divided into two parts,StartSection andEndPart. 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. Let's focus on that.Start
Part ofFStart
AndC.Start
。C.Start
can also be omitted, the omitted words are indicated from the beginning of the domain.FStart.CStart
, whereFStart
Is the domain that represents the use, andCStart
It means that theFStart
The "Sort first character" is counted from the first character in the field. Similarly, in the end section, you can setFEnd.CEnd
, if you omit.CEnd
, the end to the "end of Domain", which is the last character of the domain. Or, if you set Cend to 0 (0), it is also the end to "domain Footer".
Start with the second letter of the company's English name:
$ sort -t ' ' -k 1.2 facebook.txtbaidu 100 5000sohu 100 4500google 110 5000guge 50 3000
Used to -k 1.2
sort a string that represents the second character of the first field, starting with the last character in the field. You will find that Baidu is the number one because the second letter is a. Sohu and Google's second character are O, but Sohu's H is in front of Google's O, so the two are ranked second and third respectively. Guge can only be the fourth.
Sort only for the second letter of the company's English name, if the same is sorted by the employee's salary in descending order:
$ Sort-t ' k 1.2,1.2-nrk 3,3 facebook.txtbaidu 5000google 5000sohu 4500guge 50 3000
Because only the second letter was sorted, we used the-k 1.2,1.2
means that we "only" sort the second letter. (If you ask "I use-k 1.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. In order to sort employees ' wages, we also use-k 3,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".
The Linux sort command uses parsing