Linux系统中sort命令的常见用法

sort命令的使用非常简单。示例用法如下:

[qs@qs sort]$ cat cargo.db 
ThinkPad:USA:14000:2009:X301
ThinkPad:HongKong:10000:2008:T400
ThinkPad:USA:8000:2007:X60
HP:China:5600:2010:DM3
HP:China:12000:2010:NE808
SumSung:Korea:5400:2009:Q308
[qs@qs sort]$ 

[qs@qs sort]$ sort -t: cargo.db #—t选项指定分隔符,默认按照第一个域排序
HP:China:12000:2010:NE808
HP:China:5600:2010:DM3
SumSung:Korea:5400:2009:Q308
ThinkPad:HongKong:10000:2008:T400
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:8000:2007:X60
[qs@qs sort]$ 

[qs@qs sort]$ sort cargo.db #不指定分隔符,则分隔符是空格,因为没有空格,所以只有一个域
HP:China:12000:2010:NE808
HP:China:5600:2010:DM3
SumSung:Korea:5400:2009:Q308
ThinkPad:HongKong:10000:2008:T400
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:8000:2007:X60
[qs@qs sort]$ 
-----
[qs@qs sort]$ sort -t: -k3 cargo.db #默认按照第一个域排序,-k选项可以指定按照哪个域排序。
ThinkPad:HongKong:10000:2008:T400
HP:China:12000:2010:NE808
ThinkPad:USA:14000:2009:X301
SumSung:Korea:5400:2009:Q308
HP:China:5600:2010:DM3
ThinkPad:USA:8000:2007:X60
[qs@qs sort]$ 
-----
[qs@qs sort]$ sort -t: -k3n cargo.db #-n选项表示按照数值大小进行排序,这个选项一般不单独使用,一般放在域号后面。
SumSung:Korea:5400:2009:Q308
HP:China:5600:2010:DM3
ThinkPad:USA:8000:2007:X60
ThinkPad:HongKong:10000:2008:T400
HP:China:12000:2010:NE808
ThinkPad:USA:14000:2009:X301
[qs@qs sort]$ 

[qs@qs sort]$ sort -t: -k3 -n cargo.db #一般不这样使用
SumSung:Korea:5400:2009:Q308
HP:China:5600:2010:DM3
ThinkPad:USA:8000:2007:X60
ThinkPad:HongKong:10000:2008:T400
HP:China:12000:2010:NE808
ThinkPad:USA:14000:2009:X301
[qs@qs sort]$ 
-----
[qs@qs sort]$ sort -t: -k3 -n -r cargo.db 
ThinkPad:USA:14000:2009:X301
HP:China:12000:2010:NE808
ThinkPad:HongKong:10000:2008:T400
ThinkPad:USA:8000:2007:X60
HP:China:5600:2010:DM3
SumSung:Korea:5400:2009:Q308
[qs@qs sort]$ 

[qs@qs sort]$ sort -t: -k3nr cargo.db #-r选项用于将排序结果逆向显示
ThinkPad:USA:14000:2009:X301
HP:China:12000:2010:NE808
ThinkPad:HongKong:10000:2008:T400
ThinkPad:USA:8000:2007:X60
HP:China:5600:2010:DM3
SumSung:Korea:5400:2009:Q308
[qs@qs sort]$ 

[qs@qs sort]$ sort -t: -r cargo.db #-r选项的使用
ThinkPad:USA:8000:2007:X60
ThinkPad:USA:14000:2009:X301
ThinkPad:HongKong:10000:2008:T400
SumSung:Korea:5400:2009:Q308
HP:China:5600:2010:DM3
HP:China:12000:2010:NE808
[qs@qs sort]$ 
-----
[qs@qs sort]$ cat cargo1.db 
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:14000:2009:X301
ThinkPad:HongKong:10000:2008:T400
ThinkPad:USA:8000:2007:X60
HP:China:5600:2010:DM3
HP:China:12000:2010:NE808
SumSung:Korea:5400:2009:Q308
SumSung:Korea:5400:2009:Q308
[qs@qs sort]$ 
[qs@qs sort]$ sort -t: cargo1.db 
HP:China:12000:2010:NE808
HP:China:5600:2010:DM3
SumSung:Korea:5400:2009:Q308 #有重复的行
SumSung:Korea:5400:2009:Q308
ThinkPad:HongKong:10000:2008:T400
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:8000:2007:X60
[qs@qs sort]$ 
[qs@qs sort]$ sort -t: -u cargo1.db #-u选项用于去除排序结果中的重复行
HP:China:12000:2010:NE808
HP:China:5600:2010:DM3
SumSung:Korea:5400:2009:Q308
ThinkPad:HongKong:10000:2008:T400
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:8000:2007:X60
[qs@qs sort]$ 
-----
[qs@qs sort]$ sort -t: -k3n -o sort_option_o.db cargo.db #sort命令默认将排序结果显示到屏幕上,如果需要将结果保存到另外一个文件中,我们可以使用-o选项加上文件名来完成。
[qs@qs sort]$ cat sort_option_o.db 
SumSung:Korea:5400:2009:Q308
HP:China:5600:2010:DM3
ThinkPad:USA:8000:2007:X60
ThinkPad:HongKong:10000:2008:T400
HP:China:12000:2010:NE808
ThinkPad:USA:14000:2009:X301
[qs@qs sort]$ 
-----
[qs@qs sort]$ cat cargo.db 
ThinkPad:USA:14000:2009:X301
ThinkPad:HongKong:10000:2008:T400
ThinkPad:USA:8000:2007:X60
HP:China:5600:2010:DM3
HP:China:12000:2010:NE808
SumSung:Korea:5400:2009:Q308
[qs@qs sort]$ 
[qs@qs sort]$ sort -t: -c cargo.db #-c选项测试文件是否已经排序(默认方式)
sort: cargo.db:2: disorder: ThinkPad:HongKong:10000:2008:T400
[qs@qs sort]$ 
[qs@qs sort]$ cat sort_option_o.db 
SumSung:Korea:5400:2009:Q308
HP:China:5600:2010:DM3
ThinkPad:USA:8000:2007:X60
ThinkPad:HongKong:10000:2008:T400
HP:China:12000:2010:NE808
ThinkPad:USA:14000:2009:X301
[qs@qs sort]$ 
[qs@qs sort]$ sort -t: -k3n -c sort_option_o.db #测试文件是否按照指定方式排序
[qs@qs sort]$ 
-----
[qs@qs sort]$ cat cargo2.db 
DELL:USA:6700:2009:XPS
MACBOOK:USA:10189:2010:MB991ZP/A
[qs@qs sort]$ 
[qs@qs sort]$ cat sorted_cargo.db 
HP:China:12000:2010:NE808
HP:China:12000:2010:NE808
HP:China:5600:2010:DM3
HP:China:5600:2010:DM3
SumSung:Korea:5400:2009:Q308
SumSung:Korea:5400:2009:Q308
ThinkPad:HongKong:10000:2008:T400
ThinkPad:HongKong:10000:2008:T400
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:8000:2007:X60
ThinkPad:USA:8000:2007:X60
[qs@qs sort]$ 
[qs@qs sort]$ sort -t: -m cargo2.db sorted_cargo.db #-m选项用来合并两个已经排好序的文件
DELL:USA:6700:2009:XPS
HP:China:12000:2010:NE808
HP:China:12000:2010:NE808
HP:China:5600:2010:DM3
HP:China:5600:2010:DM3
MACBOOK:USA:10189:2010:MB991ZP/A
SumSung:Korea:5400:2009:Q308
SumSung:Korea:5400:2009:Q308
ThinkPad:HongKong:10000:2008:T400
ThinkPad:HongKong:10000:2008:T400
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:8000:2007:X60
ThinkPad:USA:8000:2007:X60
[qs@qs sort]$ 
-----

你可能感兴趣的:(linux,sort)