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后面指定参数的格式:

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、例子

这里的例子用下面的文件作为例子:

$ 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选项的使用

$ 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

2.2 -k字段的使用

(1)根据人名列表中出生年份字段排序

出生年份就是出生年月的前四个字符。

$ 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 Lyrics
Timothy Theodore Duncan:male:NBA player:19760425:2.11m:San Antonio Spurs
Manu Ginóbili:male:NBA player:19770728:1.98m:San Antonio Spurs
Kobe Bryant:male:NBA player:19780823:1.98m:Los Angeles Lakers
Abbie Cornishi:female:actor:19820807:1.73m:Klondike
Tony Parker:male:NBA player:19820517:1.88m:San Antonio Spurs
LaMarcus Nurae Aldridge:male:NBA player:19850719:2.11m:Portland Trail Blazers
Hara Mikie:female:pictorial model:19870703:1.62m:Oscar Promotion Co., Ltd.
(2)根据人名列表中出生月份字段排序
出生月份就是人名列表中出生年月字段的第5到第6字符。
$ 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 Lyrics
Timothy Theodore Duncan:male:NBA player:19760425:2.11m:San Antonio Spurs
Tony Parker:male:NBA player:19820517:1.88m:San Antonio Spurs
Hara Mikie:female:pictorial model:19870703:1.62m:Oscar Promotion Co., Ltd.
LaMarcus Nurae Aldridge:male:NBA player:19850719:2.11m:Portland Trail Blazers
Manu Ginóbili:male:NBA player:19770728:1.98m:San Antonio Spurs
Abbie Cornishi:female:actor:19820807:1.73m:Klondike
Kobe Bryant:male:NBA player:19780823:1.98m:Los Angeles Lakers
(3)根据身高排序
$ 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 Lyrics
Abbie Cornishi:female:actor:19820807:1.73m:Klondike
Tony Parker:male:NBA player:19820517:1.88m:San Antonio Spurs
Kobe Bryant:male:NBA player:19780823:1.98m:Los Angeles Lakers
Manu Ginóbili:male:NBA player:19770728:1.98m:San Antonio Spurs
LaMarcus Nurae Aldridge:male:NBA player:19850719:2.11m:Portland Trail Blazers
Timothy Theodore Duncan:male:NBA player:19760425:2.11m:San Antonio Spurs
到这里,指定字段的方法应该就说完了,以后用到更加复杂的仍会贴在这里。^_^

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。