Use of the sort command in Linux
By default, the sort command in Linux sorts each row in the file and then outputs the result. Specifically, it starts from the first character of each line and compares the results by ASCII code value in sequence. Finally, the results are output in ascending order.
1. Common sort options
- -U ignores the same rows during sorting, similar to the uniq command. However, the uniq command can only skip adjacent identical rows and cannot sort.
- -N is sorted by number size, rather than the text ascii code order.
- -R reverse order, that is, descending order.
- -T specifies the column separator. Note that the separator can only be one character, not multiple characters,-t, -- field-separator = SEP use SEP instead of non-blank to blank transition. The column Delimiter is separated by non-null to null by default.
- -K specifies the column for sorting keywords.
- -S sometimes, sort automatically performs resort based on other fields after sorting the specified fields. To ensure the order of the original data records, you can use the-s option to disable the resort mechanism of the sort command.
Here, the-k option is more complex. Here we will separately describe the format of the parameter specified after-k:
FStart. CStartModifier, FEnd. CEndModifier
Start and end
The-k option is used to specify the keyword column for sorting, that is, the field based on sorting. The parameter format after-k is roughly divided into two parts: the start part and the end part. The Start part shows three parameters: FStart, CStart, and Modifier. FStart indicates the column (FStart indicates Field Start, that is, this value is used to specify the column starting from which the sorting is based. The column sequence number starts from 1, not the usual 0 used by programmers. CStart indicates the Character (Character Start, that is to say, this value indicates that the sorting starts from the first character of the column. If it is not specified, it starts from the first character by default ), modifier is the option for n, r, and other specified numerical sorting and reverse order. Similar to the start part, the end part also has three parameters: FEnd, CEnd, and Modifier. FEnd indicates that the sorting ends Based on the specified column, CEnd specifies that the sorting ends based on the characters in the column. If it is omitted or specified as 0, the last character in the field is used by default. Modifier has the same meaning as the start part.
NOTE: If no start part exists, the end part starts from the beginning of the row by default. If the end part is omitted, the end part ends at the end of the row by default.
2. Example
The example here uses the following file as an example:
$ Cat name_list.txt
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
Abbie Cornishi: female: actor: 19820807: 1.73 m: Klondike
Hara Mikie: female: pictorial model: 19870703: 1.62 m: Oscar Promotion Co., Ltd.
Drew Barrymore: female: actor: 19750222: 1.63 m: Music and Lyrics
Kobe Bryant: male: NBA player: 19780823: 1.98 m: Los Angeles Lakers
Timothy Theodore Duncan: male: NBA player: 19760425: 2.11 m: San Antonio Spurs
Tony Parker: male: NBA player: 19820517: 1.88 m: San Antonio Spurs
Manu Gin Ardo bili: male: NBA player: 19770728: 1.98 m: San Antonio Spurs
LaMarcus Nurae Aldridge: male: NBA player: 19850719: 2.11 m: Portland Trail Blazers
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
$
2.1 common r, u, and n options
$ Sort name_list.txt
Abbie Cornishi: female: actor: 19820807: 1.73 m: Klondike
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
Drew Barrymore: female: actor: 19750222: 1.63 m: Music and Lyrics
Hara Mikie: female: pictorial model: 19870703: 1.62 m: Oscar Promotion Co., Ltd.
Kobe Bryant: male: NBA player: 19780823: 1.98 m: Los Angeles Lakers
LaMarcus Nurae Aldridge: male: NBA player: 19850719: 2.11 m: Portland Trail Blazers
Manu Gin Ardo bili: male: NBA player: 19770728: 1.98 m: San Antonio Spurs
Timothy Theodore Duncan: male: NBA player: 19760425: 2.11 m: San Antonio Spurs
Tony Parker: male: NBA player: 19820517: 1.88 m: San Antonio Spurs
$ Sort-u name_list.txt
Abbie Cornishi: female: actor: 19820807: 1.73 m: Klondike
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
Drew Barrymore: female: actor: 19750222: 1.63 m: Music and Lyrics
Hara Mikie: female: pictorial model: 19870703: 1.62 m: Oscar Promotion Co., Ltd.
Kobe Bryant: male: NBA player: 19780823: 1.98 m: Los Angeles Lakers
LaMarcus Nurae Aldridge: male: NBA player: 19850719: 2.11 m: Portland Trail Blazers
Manu Gin Ardo bili: male: NBA player: 19770728: 1.98 m: San Antonio Spurs
Timothy Theodore Duncan: male: NBA player: 19760425: 2.11 m: San Antonio Spurs
Tony Parker: male: NBA player: 19820517: 1.88 m: San Antonio Spurs
$ Sort-r name_list.txt
Tony Parker: male: NBA player: 19820517: 1.88 m: San Antonio Spurs
Timothy Theodore Duncan: male: NBA player: 19760425: 2.11 m: San Antonio Spurs
Manu Gin Ardo bili: male: NBA player: 19770728: 1.98 m: San Antonio Spurs
LaMarcus Nurae Aldridge: male: NBA player: 19850719: 2.11 m: Portland Trail Blazers
Kobe Bryant: male: NBA player: 19780823: 1.98 m: Los Angeles Lakers
Hara Mikie: female: pictorial model: 19870703: 1.62 m: Oscar Promotion Co., Ltd.
Drew Barrymore: female: actor: 19750222: 1.63 m: Music and Lyrics
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
Abbie Cornishi: female: actor: 19820807: 1.73 m: Klondike
$ Sort-ru name_list.txt
Tony Parker: male: NBA player: 19820517: 1.88 m: San Antonio Spurs
Timothy Theodore Duncan: male: NBA player: 19760425: 2.11 m: San Antonio Spurs
Manu Gin Ardo bili: male: NBA player: 19770728: 1.98 m: San Antonio Spurs
LaMarcus Nurae Aldridge: male: NBA player: 19850719: 2.11 m: Portland Trail Blazers
Kobe Bryant: male: NBA player: 19780823: 1.98 m: Los Angeles Lakers
Hara Mikie: female: pictorial model: 19870703: 1.62 m: Oscar Promotion Co., Ltd.
Drew Barrymore: female: actor: 19750222: 1.63 m: Music and Lyrics
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
Abbie Cornishi: female: actor: 19820807: 1.73 m: Klondike
2.2-k Field Usage
(1) sort by the year of birth field in the person name list
The Year of birth is the first four characters of the year of birth.
$ Sort-t:-k 4, 4.4n name_list.txt
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
Drew Barrymore: female: actor: 19750222: 1.63 m: Music and Lyrics
Timothy Theodore Duncan: male: NBA player: 19760425: 2.11 m: San Antonio Spurs
Manu Gin Ardo bili: male: NBA player: 19770728: 1.98 m: San Antonio Spurs
Kobe Bryant: male: NBA player: 19780823: 1.98 m: Los Angeles Lakers
Abbie Cornishi: female: actor: 19820807: 1.73 m: Klondike
Tony Parker: male: NBA player: 19820517: 1.88 m: San Antonio Spurs
LaMarcus Nurae Aldridge: male: NBA player: 19850719: 2.11 m: Portland Trail Blazers
Hara Mikie: female: pictorial model: 19870703: 1.62 m: Oscar Promotion Co., Ltd.
(2) sort by the birth month field in the person name list
The birth month contains 5th to 6th characters in the birthdate field in the person name list.
$ Sort-t:-k 4.5, 4.6n name_list.txt
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
Drew Barrymore: female: actor: 19750222: 1.63 m: Music and Lyrics
Timothy Theodore Duncan: male: NBA player: 19760425: 2.11 m: San Antonio Spurs
Tony Parker: male: NBA player: 19820517: 1.88 m: San Antonio Spurs
Hara Mikie: female: pictorial model: 19870703: 1.62 m: Oscar Promotion Co., Ltd.
LaMarcus Nurae Aldridge: male: NBA player: 19850719: 2.11 m: Portland Trail Blazers
Manu Gin Ardo bili: male: NBA player: 19770728: 1.98 m: San Antonio Spurs
Abbie Cornishi: female: actor: 19820807: 1.73 m: Klondike
Kobe Bryant: male: NBA player: 19780823: 1.98 m: Los Angeles Lakers
(3) sort by height
$ Sort-t:-k 5, 5.4n name_list.txt
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
AName: aGender: aProfession: aBirthday: aHeight: aEtc.
Hara Mikie: female: pictorial model: 19870703: 1.62 m: Oscar Promotion Co., Ltd.
Drew Barrymore: female: actor: 19750222: 1.63 m: Music and Lyrics
Abbie Cornishi: female: actor: 19820807: 1.73 m: Klondike
Tony Parker: male: NBA player: 19820517: 1.88 m: San Antonio Spurs
Kobe Bryant: male: NBA player: 19780823: 1.98 m: Los Angeles Lakers
Manu Gin Ardo bili: male: NBA player: 19770728: 1.98 m: San Antonio Spurs
LaMarcus Nurae Aldridge: male: NBA player: 19850719: 2.11 m: Portland Trail Blazers
Timothy Theodore Duncan: male: NBA player: 19760425: 2.11 m: San Antonio Spurs
At this point, the method for specifying fields should be finished, and more complex fields will be pasted here in the future. Pai_^
This article permanently updates the link address: