首页 > 系统相关 >shell中sort的用法

shell中sort的用法

时间:2022-10-25 10:59:22浏览次数:57  
标签:sort shell USA 用法 China ThinkPad 2009 8000 2010

练习文本

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

相关文章

  • efcore 连接SqlServer2008R2报错:'OFFSET' 附近有语法错误。 在 FETCH 语句中选项 NEXT
    用的是EFCore6,连接SqlServer2008R2时,生成的分页方法会报错,只需要指定ProviderName时加上版本号就行:Microsoft.EntityFrameworkCore.SqlServer@2008,高于2008版本就按默......
  • python中pip的用法
    1、安装库1pipinstall库名2、指定安装库的版本1pipinstall库名==版本3、将库更新到最新版本1pipinstall--upgrade库名4、将库更新到指定版本1pip......
  • Python——sorted自定义对一维二维数组排序
    一维数组arr=['15:30','16:30','10:00','8:00','9:00','13:30','14:30','11:00']#使用lamda自定义规则进行排序sort_arr=sorted(arr,key=lambdax:int(x......
  • [CF1753C]Wish I Knew How to Sort
    做题时间:2022.10.25\(【题目描述】\)给定一个长度为\(n\)的01序列\(a\)和一种操作,你需要用这种操作将序列从小到大排序。操作为:等概率随机选择两个位置\(i,j(i<j)\)......
  • tensorflow中session的用法,莫烦的例子(2)
    importtensorflowastf#创建两个矩阵maxtrix,然后输出两个矩阵相乘的结果matrix1=tf.constant([[3,3]])#constant表示maxtrix1是一个常数,[3,3]表示是一个一行两列的......
  • Go语言输出函数fmt.Print、fmt.Printf、fmt.Println的用法区别
    fmt包的介绍fmt=format,是一种格式化输出函数汇总包,用于格式化输出fmt.Print===原样输出Printformatsusingthedefaultformatsforitsoperandsandwritesto......
  • session的用法
    seession是一个为了保存用户状态而创建的一个特殊的对象其本质上相当于Map,里面可以存放多个键值对,是以key-value进行存放的(key必须是一个字符串,value是一个对象)该对象主......
  • shell编程之函数,递归
    函数定义函数格式一:function函数名{命令序列}格式二:函数名(){命令序列}#####main#####可以直接在主代码区直接使用函数名调用函数   删除函数格式:u......
  • shell脚本之函数
    一、函数概念及格式1、函数的概念函数定义:封装的可重复利用的具有特定功能的代码函数就是可以在shell脚本当中做出一个类似自定义执行的命令的东西,最大的功能就是简化我......
  • maven生命周期及命令用法
    生命周期一个项目的构建build的过程通常包括:清理、编译、测试、打包、部署等Maven定义了三套生命周期:clean:处理项目的清理工作,主要命令是cleandefault:处理项......