Sort command
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.
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: 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: Sorts;-t< delimited characters in reverse order;: Specifies the field separator character to use when sorting;+< start >-< end field;: Sorts by the specified field, 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 protected ] text]# sort-u sort.txtaaa:10:1.1bbb:20:2.2ccc:30:3.3ddd:40:4.4eee:50:5.5 or [[email protected] text]# Uniq sort.txtaaa:10:1.1ccc:30:3.3ddd:40:4.4bbb:20:2.2eee:50:5.5
Use of the-N,-R,-K,-t options for 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# Arrange 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 large to 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 that needs to be sorted,-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 , divided into two parts, theStart part and the End part of the comma. 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're talking about Start part of the FStart and C.Start . C.Startcan also be omitted, the omitted words are indicated from the beginning of the domain. FStart.CStart, which FStart is the field that represents the use, and the CStart FStart "sort first character" from the first character in the 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".
Start with the second letter of the company's English name:
$ sort-t "-1.2 facebook.txtbaidu 5000sohu 4500google 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 is sorted, we use -k 1.2,1.2 the representation that we "only" sort the second letter. (If you ask, " -k 1.2 how can I not use it?" ", 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 a sort of employee's salary, -k 3,3 which is the most accurate statement, which means that we "only" sort the domain, because if you omit the next 3, then we "sort the contents of the beginning of the 3rd field to the last domain location".
Sorting Linux Files sort