歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

Linux下sort命令的使用

Linux下的sort命令默認將文件中的每一行為單位,進行排序,然後輸出。具體地,就是從每行的第一個字符開始,依次按ASCII碼值進行比較,最後將結果按升序排序輸出。

1、sort常用選項

 

-u 在排序的時候忽略相同的行,類似於uniq命令,不過uniq命令只能跳過相鄰的相同行,並不能進行排序。-n 按照數字大小排序,而不是文本的ascii碼順序。-r 反序,也就是降序。-t 指定列分隔符,注意這裡的分隔符只能是一個字符,不能是多個字符,-t, --field-separator=SEP use SEP instead of non-blank to blank transition,默認以非空到空的分隔作為列分隔符。-k 指定排序關鍵字的列。-s 有時候,sort會在根據指定字段排完序之後,自動根據其他字段進行resort,為了保證原來的數據記錄的順序,可以使用-s選項來禁止sort命令的resort機制。

這裡的-k選項比較復雜,這裡單獨說一下-k後面指定參數的格式:

? 1 2 FStart.CStartModifier,FEnd.CEndModifier      開始部分        ,     結束部分

-k選項用來指定排序的關鍵字列,也就是排序依據的字段。-k後的參數格式大致分為兩個部分,開始部分和結束部分。開始部分中又可以看出三個參數:FStart、CStart和Modifier,其中FStart表示第第幾列(FStart表示Field Start,也就是這個值用來指定排序依據所開始的列,列的序號從1開始,不是程序猿通常習慣的0),CStart表示第幾個字符(Character Start,就是說這個值用來說明排序依據從該列的第幾個字符開始,如果沒有指定,默認從第一個字符開始),Modifier就是n、r等指定數值排序、逆序等的選項。與開始部分類似,結束部分也有三個參數:FEnd、CEnd和Modifier,其中FEnd就是指定排序依據到第幾列結束,CEnd指定排序依據到該列的第幾個字符結束,如果省略或者指定為0,則默認到該域的最後一個字符,Modifier的含義和開始部分一樣。

注意,如果沒有開始部分,則默認從行首開始,如果省略結束部分,則默認到行末結束。

2、例子

這裡的例子用下面的文件作為例子:

 

? 1 2 3 4 5 6 7 8 9 10 11 12 13 $ cat name_list.txt aName:aGender:aProfession:aBirthday:aHeight:aEtc. aName:aGender:aProfession:aBirthday:aHeight:aEtc. Abbie Cornishi:female:actor:19820807:1.73m:Klondike Hara Mikie:female:pictorial model:19870703:1.62m:Oscar Promotion Co., Ltd. Drew Barrymore:female:actor:19750222:1.63m:Music and Lyrics Kobe Bryant:male:NBA player:19780823:1.98m:Los Angeles Lakers Timothy Theodore Duncan:male:NBA player:19760425:2.11m:San Antonio Spurs Tony Parker:male:NBA player:19820517:1.88m:San Antonio Spurs Manu Ginóbili:male:NBA player:19770728:1.98m:San Antonio Spurs LaMarcus Nurae Aldridge:male:NBA player:19850719:2.11m:Portland Trail Blazers aName:aGender:aProfession:aBirthday:aHeight:aEtc. $

 

2.1 常見的r,u,n選項的使用

 

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 $ sort name_list.txt Abbie Cornishi:female:actor:19820807:1.73m: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.63m:Music and Lyrics Hara Mikie:female:pictorial model:19870703:1.62m:Oscar Promotion Co., Ltd. Kobe Bryant:male:NBA player:19780823:1.98m:Los Angeles Lakers LaMarcus Nurae Aldridge:male:NBA player:19850719:2.11m:Portland Trail Blazers Manu Ginóbili:male:NBA player:19770728:1.98m:San Antonio Spurs Timothy Theodore Duncan:male:NBA player:19760425:2.11m:San Antonio Spurs Tony Parker:male:NBA player:19820517:1.88m:San Antonio Spurs   $ sort -u name_list.txt Abbie Cornishi:female:actor:19820807:1.73m:Klondike aName:aGender:aProfession:aBirthday:aHeight:aEtc. Drew Barrymore:female:actor:19750222:1.63m:Music and Lyrics Hara Mikie:female:pictorial model:19870703:1.62m:Oscar Promotion Co., Ltd. Kobe Bryant:male:NBA player:19780823:1.98m:Los Angeles Lakers LaMarcus Nurae Aldridge:male:NBA player:19850719:2.11m:Portland Trail Blazers Manu Ginóbili:male:NBA player:19770728:1.98m:San Antonio Spurs Timothy Theodore Duncan:male:NBA player:19760425:2.11m:San Antonio Spurs Tony Parker:male:NBA player:19820517:1.88m:San Antonio Spurs   $ sort -r name_list.txt Tony Parker:male:NBA player:19820517:1.88m:San Antonio Spurs Timothy Theodore Duncan:male:NBA player:19760425:2.11m:San Antonio Spurs Manu Ginóbili:male:NBA player:19770728:1.98m:San Antonio Spurs LaMarcus Nurae Aldridge:male:NBA player:19850719:2.11m:Portland Trail Blazers Kobe Bryant:male:NBA player:19780823:1.98m:Los Angeles Lakers Hara Mikie:female:pictorial model:19870703:1.62m:Oscar Promotion Co., Ltd. Drew Barrymore:female:actor:19750222:1.63m: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.73m:Klondike   $ sort -ru name_list.txt Tony Parker:male:NBA player:19820517:1.88m:San Antonio Spurs Timothy Theodore Duncan:male:NBA player:19760425:2.11m:San Antonio Spurs Manu Ginóbili:male:NBA player:19770728:1.98m:San Antonio Spurs LaMarcus Nurae Aldridge:male:NBA player:19850719:2.11m:Portland Trail Blazers Kobe Bryant:male:NBA player:19780823:1.98m:Los Angeles Lakers Hara Mikie:female:pictorial model:19870703:1.62m:Oscar Promotion Co., Ltd. Drew Barrymore:female:actor:19750222:1.63m:Music and Lyrics aName:aGender:aProfession:aBirthday:aHeight:aEtc. Abbie Cornishi:female:actor:19820807:1.73m:Klondike
Copyright © Linux教程網 All Rights Reserved