练习文本
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
IdeaPad:China:8000:2007:U450
Acer:Taiwan:8000:2010:PT210
-t:制定分隔符分隔符 首先根据第一域排序,如果第一域相同久根据第二域排序
sort -t ':' cargo.db
Acer:Taiwan:8000:2010:PT210
HP:China:12000:2010:NE808
HP:China:5600:2010:DM3
IdeaPad:China:8000:2007:U450
SumSung:Korea:5400:2009:Q308
ThinkPad:HongKong:10000:2008:T400
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:8000:2007:X60
[root@oraclehost shell]# sort -t: cargo.db
Acer:Taiwan:8000:2010:PT210
HP:China:12000:2010:NE808
HP:China:5600:2010:DM3
IdeaPad:China:8000:2007:U450
SumSung:Korea:5400:2009:Q308
ThinkPad:HongKong:10000:2008:T400
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:8000:2007:X60
-k:指定排序的域号
sort -t: -k2 cargo.db
HP:China:12000:2010:NE808
HP:China:5600:2010:DM3
IdeaPad:China:8000:2007:U450
ThinkPad:HongKong:10000:2008:T400
SumSung:Korea:5400:2009:Q308
Acer:Taiwan:8000:2010:PT210
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:8000:2007:X60
sort -t: -k3 cargo.db//按照第三域排序,但是可以看到并不是按照数字大小进行排序,而是按照字符串的方式以首字母排序,相同再按照第二个字母排序,以此类推
sort -t: -k3 cargo.db
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
IdeaPad:China:8000:2007:U450
ThinkPad:USA:8000:2007:X60
Acer:Taiwan:8000:2010:PT210
-n:指定数字大小进行排序
默认的-k3就会按照字符串的方式进行排序,加上-n就会将第三域按照数字进行排序 -n一般不单独使用,跟在-k域号之后
sort -t: -k3n cargo.db
SumSung:Korea:5400:2009:Q308
HP:China:5600:2010:DM3
Acer:Taiwan:8000:2010:PT210
IdeaPad:China:8000:2007:U450
ThinkPad:USA:8000:2007:X60
ThinkPad:HongKong:10000:2008:T400
HP:China:12000:2010:NE808
ThinkPad:USA:14000:2009:X301
-r:排序结果逆向展示,-r一般也不单独使用,跟在-k域号之后
sort -t: -k3nr cargo.db
ThinkPad:USA:14000:2009:X301
HP:China:12000:2010:NE808
ThinkPad:HongKong:10000:2008:T400
Acer:Taiwan:8000:2010:PT210
IdeaPad:China:8000:2007:U450
ThinkPad:USA:8000:2007:X60
HP:China:5600:2010:DM3
SumSung:Korea:5400:2009:Q308
-u:去除排序结果中的重复行
sort -t: cargo.db
Acer:Taiwan:8000:2010:PT210
HP:China:12000:2010:NE808
HP:China:5600:2010:DM3
IdeaPad:China:8000:2007:U450
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
sort -t: -u cargo.db //去重后的结果
Acer:Taiwan:8000:2010:PT210
HP:China:12000:2010:NE808
HP:China:5600:2010:DM3
IdeaPad:China:8000:2007:U450
SumSung:Korea:5400:2009:Q308
ThinkPad:HongKong:10000:2008:T400
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:8000:2007:X60
-o:sort完成后会将结果输出在屏幕上,如果想要将结果输入到文件中,则可以使用-o来完成,类似于IO重定向
sort -t: -u -o relsult.db cargo.db
cat relsult.db
Acer:Taiwan:8000:2010:PT210
HP:China:12000:2010:NE808
HP:China:5600:2010:DM3
IdeaPad:China:8000:2007:U450
SumSung:Korea:5400:2009:Q308
ThinkPad:HongKong:10000:2008:T400
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:8000:2007:X60
-c:判断文件中内容是否排好序
cat cargo.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
IdeaPad:China:8000:2007:U450
Acer:Taiwan:8000:2010:PT210
sort -t: -c cargo.db//前三行中,ThinkPad都一样,后面的Hongkong的H应该在USA的U前面,所以判定未排序,输出解释为sort,文件名,未排序的行号,未排序,未排序的值
sort: cargo.db:3: disorder: ThinkPad:HongKong:10000:2008:T400
cat cargo.db
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:8000:2007:X60
ThinkPad:HongKong:10000:2008:T400//改行调整后再次进行判断
HP:China:5600:2010:DM3
HP:China:12000:2010:NE808
SumSung:Korea:5400:2009:Q308
SumSung:Korea:5400:2009:Q308
IdeaPad:China:8000:2007:U450
Acer:Taiwan:8000:2010:PT210
sort -t: -c cargo.db
sort: cargo.db:4: disorder: ThinkPad:HongKong:10000:2008:T400
之前排序过的文件输出到了result.db中,所以判断该文件是已排序,已排序的文件就不会有任何输出
cat relsult.db
Acer:Taiwan:8000:2010:PT210
HP:China:12000:2010:NE808
HP:China:5600:2010:DM3
IdeaPad:China:8000:2007:U450
SumSung:Korea:5400:2009:Q308
ThinkPad:HongKong:10000:2008:T400
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:8000:2007:X60
sort -t: -c relsult.db
-m:将两个排好序的文件合并成一个排好序的文件,-m对未排序的文件合并是毫无意义的
cat cargo2.db
DELL:USA:6700:2009:XPS
MACBOOK:USA:10198:2010:MB991ZP/A
cat relsult.db
Acer:Taiwan:8000:2010:PT210
HP:China:12000:2010:NE808
HP:China:5600:2010:DM3
IdeaPad:China:8000:2007:U450
SumSung:Korea:5400:2009:Q308
ThinkPad:HongKong:10000:2008:T400
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:8000:2007:X60
sort -t: -m cargo2.db relsult.db
Acer:Taiwan:8000:2010:PT210
DELL:USA:6700:2009:XPS
HP:China:12000:2010:NE808
HP:China:5600:2010:DM3
IdeaPad:China:8000:2007:U450
MACBOOK:USA:10198:2010:MB991ZP/A
SumSung:Korea:5400:2009:Q308
ThinkPad:HongKong:10000:2008:T400
ThinkPad:USA:14000:2009:X301
ThinkPad:USA:8000:2007:X60
标签:sort,shell,USA,用法,China,ThinkPad,2009,8000,2010
From: https://www.cnblogs.com/nanblog/p/16824098.html