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后面指定参数的格式:?12FStart.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、例子这里的例子用下面的文件作为例子: ?12345678910111213$ 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 常见的r,u,n选项的使用 ?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647$ 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字段的使用(1)根据人名列表中出生年份字段排序出生年份就是出生年月的前四个字符。?123456789101112$ 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)根据人名列表中出生月份字段排序出生月份就是人名列表中出生年月字段的第5到第6字符。?123456789101112$ 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)根据身高排序?123456789101112$ 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到这里,指定字段的方法应该就说完了,以后用到更加复杂的仍会贴在这里。^_^
以上就是系统大全给大家介绍的如何使的方法都有一定的了解了吧,好了,如果知识兔大家还想了解更多的资讯,那就赶紧点击系统大全官网吧。
本文来自系统大全http://www.win7cn.com/如需转载请注明!推荐:win7纯净版
下载仅供下载体验和测试学习,不得商用和正当使用。