Using the sort command under Linux

Source: Internet
Author: User
Tags modifier sorts

The sort command under Linux, by default, sorts each unit of behavior in the file, and then outputs it. Specifically, it starts with the first character of each line, then compares it by the ASCII value, and finally sorts the output in ascending order.

1. Sort Common options

    • -U ignores the same rows when sorting, similar to the Uniq command, but the Uniq command can only skip adjacent identical rows and cannot be sorted.
    • -N is sorted by numeric size instead of the ASCII order of the text.
    • -R Inverse order, that is, descending.
    • -t specifies the column delimiter, note that here the delimiter can only be one character, cannot be more than one character,-T,--field-separator=sep use SEP instead of the Non-blank to blank transition, The default is a non-empty-to-empty delimiter as a column delimiter.
    • -k Specifies the column for the sort key.
    • -S Sometimes, sort will be automatically resort according to the other fields after sorting according to the specified fields, in order to guarantee the order of the original data records, you can use the-s option to suppress the resort mechanism of the sort command.

The-K option here is more complex, and here's a separate format for specifying parameters after-K:

Fstart.cstartmodifier,fend.cendmodifier start part, end part

The-k option is used to specify the sort key column, which is the field to sort by. The parameter format after-K is roughly divided into two parts, beginning and ending. In the beginning, you can see three parameters: Fstart, Cstart, and modifier, where Fstart represents the first column (Fstart indicates field start, which is the column that the value is used to specify the sort by, and the column ordinal starts at 1. Not a program ape is usually accustomed to 0), Cstart represents the first character (Character start, which means that this value is used to indicate the sort by starting from the first character of the column, if not specified, the default starting from the beginning of the character), modifier is n, r, and other specified numeric sorting, Options for reverse order, and so on. Similar to the beginning section, the end section also has three parameters: fend, cend, and modifier, where fend is the specified sort by to the end of the first few columns, cend specifies the sort by to the end of the first character of the column, if omitted or specified as 0, the default to the last character of the field, The meaning of modifier is the same as the beginning part.

Note that if there is no start section, the default starts at the beginning of the line, and if the end part is omitted, the default ends at the end of the row.

2. Example

Here's an example using the following file as an example:

2.1 Common use of the R,U,N option

$ 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:o Scar 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

Use of the 2.2-k field

(1) Sort by the year of birth in the list of people

The year of birth is the first four characters of the date 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 month of birth in the list of people
The month of birth is the 5th to 6th character of the Birth date field in the list of people.
$ 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
Here, the method to specify the field should be finished, and later use more complex will still be posted here. ^_^

Using the sort command under Linux

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.