Linux sort排序方法

avatar 2020年2月21日18:44:07 评论 1,502 次浏览

在文件的操作过程中,因为文件过多,往往需要进行一下排序,排序方法也就是从小到大排序或者从大到小排序。比如我们从nginx日志中需要找到访问量最长的url,那就需要对请求时间进行一个排序,根据请求时间长短排序后在打印后面的url就能清楚的知道那个url有问题了,废话先不说,看方法:

sort排序

文件排序我们先说一下linux的sort命令,sort命令可以根据我们的需求完成从大到小或者从小到大的排序。注意:sort是针对文件内容,以行为单位来排序。先看一下sort命令格式:

sort [参数] file

参数详解:

-b   会忽略每一行前面的所有空白部分,从第一个可见字符开始比较。
-d:按照字典顺序排序,只支持字母、数值、空白。除了特殊字符,一般情况下基本等同于默认排序规则。
--debug:将显示排序的过程以及每次排序所使用的字段、字符。同时还会在最前几行显示额外的信息。
-c   会检查文件是否已排好序,如果乱序,则输出第一个乱序的行的相关信息,最后返回1。
-C   会检查文件是否已排好序,如果乱序,不输出内容,仅返回1
-u   只输出重复行的第一行,结合'-f'使用时,重复的小写会被丢弃。
-f   将所有小写转大写,和'-u'一起使用时,如果排序的比较结果相等,则丢弃小写字母行。
-M   会以月份来排序,比如JAN小于FEB等等
-n   依照数值排序,遇到不识别的字符立即结束该Key的排序。有字符串""或"\0"被当作空,该选项除了能识别负号"-",其他所有非数字字符都不识别。
-o<输出文件>   将排序后的结果存入指定的文件。
-r  排序后的反序排列,不参与排序动作。
-s:禁止sort做"最后的排序"。
-t<分隔字符>   指定排序时所用的栏位分隔字符。
-k  选择哪个列进行排序,如果有分隔符必须参考分隔符一起使用。

为了方便举例,我们先创建一个文件,然后针对文件根据上面的参数举例说明一下:

[root@localhost wulaoer]# cat wulaoer.txt 
2       python3 800     Jan
1       Linux   1200    Mar
4       golong  800     Oct
6       DevOps  300     May
3       Ruby    200     Dec
5       redis   100     Sept

sort默认命令是从小到大排序,如果有多列根据第一列进行排序,如果第一列是IP地址,会根据IP地址的第一个字符进行排序。看下面的例子:

[root@localhost wulaoer]# sort wulaoer.txt 
1       Linux   1200    Mar
2       python3 800     Jan
3       Ruby    200     Dec
4       golong  800     Oct
5       redis   100     Sept
6       DevOps  300     May

参数-n是根据第一列的数字从小到大排序。

[root@localhost wulaoer]# sort -n wulaoer.txt 
1       Linux   1200    Mar
2       python3 800     Jan
3       Ruby    200     Dec
4       golong  800     Oct
5       redis   100     Sept
6       DevOps  300     May

参数-r是已经默认加了'-n'参数,根据第一列数字从大到小排序。

[root@localhost wulaoer]# sort -r wulaoer.txt 
6       DevOps  300     May
5       redis   100     Sept
4       golong  800     Oct
3       Ruby    200     Dec
2       python3 800     Jan
1       Linux   1200    Mar

第一列和第二列之间有分隔符,所以使用参数$'\t'

[root@localhost wulaoer]#  sort -t $'\t' -k3  wulaoer.txt 
5       redis   100     Sept
1       Linux   1200    Mar
3       Ruby    200     Dec
6       DevOps  300     May
2       python3 800     Jan
4       golong  800     Oct

这里的排序1200大于200排序错误,是因为默认安装字符集排序规则,所以需要加参数'-n'

[root@localhost wulaoer]#  sort -t $'\t' -k3 -n  wulaoer.txt 
5       redis   100     Sept
3       Ruby    200     Dec
6       DevOps  300     May
2       python3 800     Jan
4       golong  800     Oct
1       Linux   1200    Mar

使用'-M'参数,对英文月份进行排序。

[root@localhost wulaoer]#  sort -t $'\t' -k4 -M  wulaoer.txt 
2       python3 800     Jan
1       Linux   1200    Mar
6       DevOps  300     May
5       redis   100     Sept
4       golong  800     Oct
3       Ruby    200     Dec

在第二列的基础上进行排序,使用第三列决胜,且以数值排序规则对第三列排序。

[root@localhost wulaoer]# sort -t $'\t' -k2 -k3 -n wulaoer.txt 
5       redis   100     Sept
3       Ruby    200     Dec
6       DevOps  300     May
2       python3 800     Jan
4      golong  800     Oct
1       Linux   1200    Mar

之所以得到第三列排序整除,是因为在默认情况下,命令行中指定的排序行为结束后,sort会做最后一次排序,这最后一次排序是对整行按照完全默认规则进行,也就是按字符集,升序排序。如果第二行第三列有重复的会根据第一行的顺序排序。

上面第二列是字母不是数值,按数值排序时,字母是不可识别的字符,遇到不可识别的字符会立即结束该字段的排序。可以使用'--debug'选项来查看排序的过程和排序时所使用的列。注意只有在Centos7上使用。

[root@localhost wulaoer]# sort --debug -t $'\t' -k3 -k2 -n wulaoer.txt 
sort: using ‘en_US.UTF-8’ sorting rules
sort: key 1 is numeric and spans multiple fields
sort: key 2 is numeric and spans multiple fields
5>redis>100>Sept
		___						# 第1次排序行为,即对"-k3"排序,此次用于排序的字段为第3列
  ^ no match for key			# 第2次排序行为,即对"-k2"排序,但显示无法匹配排序key
________________				# 默认sort总会进行最后一次排序,排序对象为整行
3>Ruby>200>Dec	
	   ___
  ^ no match for key
______________
6>DevOps>300>May
		 ___
  ^ no match for key
________________
2>python3>800>Jan
		  ___
  ^ no match for key
_________________
4>golong>800>Oct
		 ___
  ^ no match for key
________________
1>Linux>1200>Mar
		____
  ^ no match for key
________________

在第三列数字排序规则的基础上,使用第二列作为决胜属性,且默认排序规则对此列降序排序。

[root@localhost wulaoer]# sort -t $'\t' -k3n -k2r wulaoer.txt 
5       redis   100     Sept
3       Ruby    200     Dec
6       DevOps  300     May
2       python3 800     Jan
 4      golong  800     Oct
1       Linux   1200    Mar

vim排序

vim排序参数和sort排序参数是一样的,vim的排序也是在sort的基础上实现的,所以这里就不多说,没有vim需要安装,使用vim进入文件之后使用'shift+:'进入命令模式,在命令模式下执行直接修改文件,不保存不修改。

第4列数据进行排序

1,12!sort -r -n -k4.1,5

从当前行以下20行按字母顺序排序

:.,+20!sort

从第一行开始,以第三列进行排序

:4,$!sort -k 3

至此,Linux的排序基本用法已经完成,没有了看些其他的吧。

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: