Use of the sort command in Linux

Source: Internet
Author: User

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 part, end part

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.txtaName:aGender:aProfession:aBirthday:aHeight:aEtc.aName:aGender:aProfession:aBirthday:aHeight:aEtc.Abbie Cornishi:female:actor:19820807:1.73m:KlondikeHara Mikie:female:pictorial model:19870703:1.62m:Oscar Promotion Co., Ltd.Drew Barrymore:female:actor:19750222:1.63m:Music and LyricsKobe Bryant:male:NBA player:19780823:1.98m:Los Angeles LakersTimothy Theodore Duncan:male:NBA player:19760425:2.11m:San Antonio SpursTony Parker:male:NBA player:19820517:1.88m:San Antonio SpursManu Ginóbili:male:NBA player:19770728:1.98m:San Antonio SpursLaMarcus Nurae Aldridge:male:NBA player:19850719:2.11m:Portland Trail BlazersaName:aGender:aProfession:aBirthday:aHeight:aEtc.$ 

 

2.1 common r, u, and n options

 

$ sort name_list.txtAbbie Cornishi:female:actor:19820807:1.73m:KlondikeaName:aGender:aProfession:aBirthday:aHeight:aEtc.aName:aGender:aProfession:aBirthday:aHeight:aEtc.aName:aGender:aProfession:aBirthday:aHeight:aEtc.Drew Barrymore:female:actor:19750222:1.63m:Music and LyricsHara Mikie:female:pictorial model:19870703:1.62m:Oscar Promotion Co., Ltd.Kobe Bryant:male:NBA player:19780823:1.98m:Los Angeles LakersLaMarcus Nurae Aldridge:male:NBA player:19850719:2.11m:Portland Trail BlazersManu Ginóbili:male:NBA player:19770728:1.98m:San Antonio SpursTimothy Theodore Duncan:male:NBA player:19760425:2.11m:San Antonio SpursTony Parker:male:NBA player:19820517:1.88m:San Antonio Spurs$ sort -u name_list.txtAbbie Cornishi:female:actor:19820807:1.73m:KlondikeaName:aGender:aProfession:aBirthday:aHeight:aEtc.Drew Barrymore:female:actor:19750222:1.63m:Music and LyricsHara Mikie:female:pictorial model:19870703:1.62m:Oscar Promotion Co., Ltd.Kobe Bryant:male:NBA player:19780823:1.98m:Los Angeles LakersLaMarcus Nurae Aldridge:male:NBA player:19850719:2.11m:Portland Trail BlazersManu Ginóbili:male:NBA player:19770728:1.98m:San Antonio SpursTimothy Theodore Duncan:male:NBA player:19760425:2.11m:San Antonio SpursTony Parker:male:NBA player:19820517:1.88m:San Antonio Spurs$ sort -r name_list.txtTony Parker:male:NBA player:19820517:1.88m:San Antonio SpursTimothy Theodore Duncan:male:NBA player:19760425:2.11m:San Antonio SpursManu Ginóbili:male:NBA player:19770728:1.98m:San Antonio SpursLaMarcus Nurae Aldridge:male:NBA player:19850719:2.11m:Portland Trail BlazersKobe Bryant:male:NBA player:19780823:1.98m:Los Angeles LakersHara Mikie:female:pictorial model:19870703:1.62m:Oscar Promotion Co., Ltd.Drew Barrymore:female:actor:19750222:1.63m:Music and LyricsaName:aGender:aProfession:aBirthday:aHeight:aEtc.aName:aGender:aProfession:aBirthday:aHeight:aEtc.aName:aGender:aProfession:aBirthday:aHeight:aEtc.Abbie Cornishi:female:actor:19820807:1.73m:Klondike$ sort -ru name_list.txtTony Parker:male:NBA player:19820517:1.88m:San Antonio SpursTimothy Theodore Duncan:male:NBA player:19760425:2.11m:San Antonio SpursManu Ginóbili:male:NBA player:19770728:1.98m:San Antonio SpursLaMarcus Nurae Aldridge:male:NBA player:19850719:2.11m:Portland Trail BlazersKobe Bryant:male:NBA player:19780823:1.98m:Los Angeles LakersHara Mikie:female:pictorial model:19870703:1.62m:Oscar Promotion Co., Ltd.Drew Barrymore:female:actor:19750222:1.63m:Music and LyricsaName:aGender:aProfession:aBirthday:aHeight:aEtc.Abbie Cornishi:female:actor:19820807:1.73m: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.63m:Music and LyricsTimothy Theodore Duncan:male:NBA player:19760425:2.11m:San Antonio SpursManu Ginóbili:male:NBA player:19770728:1.98m:San Antonio SpursKobe Bryant:male:NBA player:19780823:1.98m:Los Angeles LakersAbbie Cornishi:female:actor:19820807:1.73m:KlondikeTony Parker:male:NBA player:19820517:1.88m:San Antonio SpursLaMarcus Nurae Aldridge:male:NBA player:19850719:2.11m:Portland Trail BlazersHara Mikie:female:pictorial model:19870703:1.62m: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.63m:Music and LyricsTimothy Theodore Duncan:male:NBA player:19760425:2.11m:San Antonio SpursTony Parker:male:NBA player:19820517:1.88m:San Antonio SpursHara Mikie:female:pictorial model:19870703:1.62m:Oscar Promotion Co., Ltd.LaMarcus Nurae Aldridge:male:NBA player:19850719:2.11m:Portland Trail BlazersManu Ginóbili:male:NBA player:19770728:1.98m:San Antonio SpursAbbie Cornishi:female:actor:19820807:1.73m:KlondikeKobe Bryant:male:NBA player:19780823:1.98m: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.62m:Oscar Promotion Co., Ltd.Drew Barrymore:female:actor:19750222:1.63m:Music and LyricsAbbie Cornishi:female:actor:19820807:1.73m:KlondikeTony Parker:male:NBA player:19820517:1.88m:San Antonio SpursKobe Bryant:male:NBA player:19780823:1.98m:Los Angeles LakersManu Ginóbili:male:NBA player:19770728:1.98m:San Antonio SpursLaMarcus Nurae Aldridge:male:NBA player:19850719:2.11m:Portland Trail BlazersTimothy Theodore Duncan:male:NBA player:19760425:2.11m: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_^

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.