Linux下sort命令的使用(2)

$ 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

到这里,指定字段的方法应该就说完了,以后用到更加复杂的仍会贴在这里。^_^

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/16919.html