简体   繁体   English

shell 脚本中的复杂排序

[英]Complex sorting in shell script

I have written a script that fulfills the extraction requirement but I cannot sort the results according to the formatting requirements.我编写了一个满足提取要求的脚本,但我无法根据格式要求对结果进行排序。 This is my script:这是我的脚本:

awk '{print $9"  "$1}' ab.log | sort | uniq -c | awk '{print $2 "\t" $3}' | sort -nr

It shows the status code and IP address in descending order;它按降序显示状态码和 IP 地址;

404   89.86.144.219
404   81.192.148.245
.
.
403   172.6.0.3
403   129.16.26.39
402   145.8.0.9
402   256.23.4.57
.
.
401   126.158.20.9

But the requirements are: status code groups have to be sorted by which status code group appears more often and the IP addresses have to be sorted by occurrence within each of the groups.但要求是:状态代码组必须按哪个状态代码组出现频率更高进行排序,并且 IP 地址必须按每个组中出现的次数进行排序。

So the output should be:所以 output 应该是:

404    127.0.0.1
404    xxx.xxx.xxx.xxx
.
.
200    xxx.xxx.xxx.xxx
200    xxx.xxx.xxx.xxx
.
.
403    xxx.xxx.xxx.xxx
403    xxx.xxx.xxx.xxx

How can I sort the result according to the above requirements?如何根据上述要求对结果进行排序?

There is probably an amazing single command you can use for this.可能有一个惊人的命令可以用于此。 However here is a pipeline using awk, sort and cut:然而这里是使用 awk 的管道,排序和剪切:

$ awk '{a[$1]++;b[$0]=$1}END{for(i in b) printf "%-9d%s\n",a[b[i]],i}' | sort -k1,1nr -k2,2n -k3,3 | cut -c9-

With GNU awk, you could even make it into a single command, but it just will start to look messy使用 GNU awk,你甚至可以把它变成一个命令,但它只会开始看起来很乱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM