Sort command Summary
Function: Sort
Syntax: sort [-bcdfimMnr] [-o <output file>] [-t <delimiter>] [+ <start column>-<End column>] [-- help] [-- verison] [file]
Parameters:
-B ignores the leading space characters in each line.
-D: When sorting, other characters are ignored except English letters, numbers, and spaces.
-F: lowercase letters are considered as uppercase letters.
-In I sorting, except for ASCII characters between 176 and, other characters are ignored.
-C: Check whether the files are sorted in order.
-M combines several sorted files.
-The first three letters of M are sorted by the abbreviation of month.
-N is sorted by the value size.
-O <output file> stores the sorted results to the specified file.
-R is sorted in reverse order.
-T <delimiter> specifies the column delimiter used for sorting.
-K specifies the domain
-- Help: displays help.
-- Version: displays the version information.
Keys [root @ Jesse ~] # Cat c.txt
7 July
8 August
9 September
10 October
11 November
12 December
1 January
2 February
3 March
4 L
5 May
6 June
[Root @ Jesse ~] # Sort-c c.txt
Sort: c.txt: 4: disorder: 10 October // It indicates that the data is not arranged in a certain order. If nothing is displayed, it indicates that the data is classified in a certain order.
Keys [root @ Jesse ~] # Cat a.txt
Google 110 5000
Baidue 100 5010
Gugeee 120 3000
Sohuee 100 4500
[Root @ Jesse ~] # Cat B .txt
3 ladygaga 1
5 ladygaga 9
0 ladygaga 5
9 ladygaga 4
6 ladygaga 2
8 ladygaga 3
7 ladygaga 0
1 ladygaga 6
4 ladygaga 7
2 ladygaga 8
[Root @ Jesse ~] # Sort-m a.txt c.txt // integrate the content of the two files
7 July
8 August
9 September
10 October
11 November
12 December
1 January
2 February
3 March
4 L
5 May
6 June
Google 110 5000
Baidue 100 5010
Gugeee 120 3000
Sohuee 100 4500
Sort by month
[Root @ Jesse ~] # Cat c.txt
July
August
September
October
November
December
January
February
March
Else l
May
June
[Root @ Jesse ~] # Sort-M c.txt
January
February
March
Else l
May
June
July
August
September
October
November
December
Keys [root @ Jesse ~] # Sort-M c.txt-o d.txt // import the content into the d.txt file.
Keys [root @ Jesse ~] # Cat e.txt
1
2
3
4
5
6
7
8
[Root @ Jesse ~] # Sort-r e.txt // sort in reverse order
8
7
6
5
4
3
2
1
Keys [root @ Jesse ~] # Cat a.txt
Google 110 5000
Baidue 100 5010
Gugeee 120 3000
Sohuee 100 4500
[Root @ Jesse ~] # Sort-t ""-k 3 a.txt // The place where spaces appear as the first field for classification
Gugeee 120 3000
Sohuee 100 4500
Google 110 5000
Baidue 100 5010
######################################## ################################ Example of-k
######################################## ################################ Preparations:
[Root @ Jesse ~] # Cat a.txt
Google 110 5000
Baidue 100 5000
Gugeee 120 3000
Sohuee 101 4500
Sort by letter. Note that there must be a space in the quotation marks after-t. The single quotation marks and double quotation marks do not matter.
[Root @ Jesse ~] # Sort-t ""-k 1 a.txt
Baidue 100 5000
Google 110 5000
Gugeee 120 3000
Sohuee 101 4500
[Root @ Jesse ~] # Sort-t'-k 1 a.txt
Baidue 100 5000
Google 110 5000
Gugeee 120 3000
Sohuee 101 4500
Sort by the second field of the split domain with spaces
[Root @ Jesse ~] # Sort-t'-k 2 a.txt
Baidue 100 5000
Sohuee 101 4500
Google 110 5000
Gugeee 120 3000
Let's test the case where the second domain is the same.
[Root @ Jesse ~] # Cat a.txt
Google 110 5000
Baidue 100 5010
Gugeee 120 3000
Sohuee 100 4500
The following example shows that when the second domain is the same, the third domain has no order. If the second domain is the same, what should we do when the third domain is also sorted?
[Root @ Jesse ~] # Sort-n-t ''-k 2 a.txt
Baidue 100 5010
Sohuee 100 4500
Google 110 5000
Gugeee 120 3000
See the following demo. If the second domain is the same, we will perform the lifting operation from the third domain.
[Root @ Jesse ~] # Sort-n-t ''-k 2-k 3 a.txt
Sohuee 100 4500
Baidue 100 5010
Google 110 5000
Gugeee 120 3000
If I want to arrange the third domain of the split domain in reverse order
[Root @ Jesse ~] # Sort-n-t'-k 3r a.txt
Baidue 100 5010
Google 110 5000
Sohuee 100 4500
Gugeee 120 3000
If I want the third domain separated by spaces to be arranged in the forward order
[Root @ Jesse ~] # Sort-t'-k 3n a.txt
Gugeee 120 3000
Sohuee 100 4500
Google 110 5000
Baidue 100 5010
If two numbers in the second field are the same, we will sort them in ascending or descending order.
[Root @ Jesse ~] # Sort-t'-k 2n-k 3n a.txt
Sohuee 100 4500
Baidue 100 5010
Google 110 5000
Gugeee 120 3000
If you want to sort from the third letter
[Root @ Jesse ~] # Sort-t'-k 1.3 a.txt
Gugeee 120 3000
Sohuee 100 4500
Baidue 100 5010
Google 110 5000
If you only want to sort by the third character of the first field:-k 1.3, 1.3 indicates that only the third letter is used for sorting.
[Root @ Jesse ~] # Sort-t'-k 1.3, 1.3 a.txt
Gugeee 120 3000
Sohuee 100 4500
Baidue 100 5010
Google 110 5000
N indicates sorting, and r indicates reverse sorting.
[Root @ Jesse ~] # Sort-t ""-k 2nr a.txt
Gugeee 120 3000
Google 110 5000
Baidue 100 5010
Sohuee 100 4500
Sort by second domain. If there is a duplicate, delete it.
[Root @ Jesse ~] # Sort-t ""-k 2n-u a.txt
Baidue 100 5010
Google 110 5000
Gugeee 120 3000
This article is from the "jesse's blog" blog