文本文件排序错误

文本文件排序错误

我有这个简单的文本文件,并且想要按列对所有行进行排序response_time

1 console-2017_09_17.log:2017-09-17 00:04:02,507:INFO :pool-8982-thread-30:r.a.u.j.PerfRequestInterceptor - method=GET, uri==43975&items, response_time=1030, response_code=300 
2 console-2017_09_17.log:2017-09-17 00:04:02,628:INFO :pool-8982-thread-77:r.a.u.j.PerfRequestInterceptor - method=GET, uri==PBD4766&items, response_time=1142, response_code=200 
3 console-2017_09_17.log:2017-09-17 00:04:02,651:INFO :pool-8982-thread-67:r.a.u.j.PerfRequestInterceptor - method=GET, uri==MDC1810C&items, response_time=1166, response_code=200 
4 console-2017_09_17.log:2017-09-17 00:04:02,655:INFO :pool-8984-thread-47:r.a.u.j.PerfRequestInterceptor - method=GET, uri==1031086&items, response_time=1005, response_code=200 
5 console-2017_09_17.log:2017-09-17 00:04:02,668:INFO :pool-8984-thread-50:r.a.u.j.PerfRequestInterceptor - method=GET, uri==F1018P&items, response_time=1018, response_code=200 
6 console-2017_09_17.log:2017-09-17 00:04:02,677:INFO :pool-8983-thread-86:r.a.u.j.PerfRequestInterceptor - method=GET, uri==V2581261&items, response_time=1060, response_code=200 
7 console-2017_09_17.log:2017-09-17 00:04:02,681:INFO :pool-8982-thread-68:r.a.u.j.PerfRequestInterceptor - method=GET, uri==202581&items, response_time=1196, response_code=200 
8 console-2017_09_17.log:2017-09-17 00:04:02,686:INFO :pool-8984-thread-46:r.a.u.j.PerfRequestInterceptor - method=GET, uri==1563200600&, response_time=1036, response_code=200 
9 console-2017_09_17.log:2017-09-17 00:04:02,693:INFO :pool-8983-thread-29:r.a.u.j.PerfRequestInterceptor - method=GET, uri==8DB355025371, response_time=1207, response_code=200

我尝试过这种方法:

sort -n -t " " -k 8 myfile.txt

但我仍然得到一个未分类的文件。为什么?

更新: 时间可以小于 1000 且大于 9999:

1 console-2017_09_17.log:2017-09-17 00:04:02,507:INFO :pool-8982-thread-30:r.a.u.j.PerfRequestInterceptor - method=GET, uri==43975&items, response_time=11030, response_code=300 
2 console-2017_09_17.log:2017-09-17 00:04:02,628:INFO :pool-8982-thread-77:r.a.u.j.PerfRequestInterceptor - method=GET, uri==PBD4766&items, response_time=1142, response_code=200 
3 console-2017_09_17.log:2017-09-17 00:04:02,651:INFO :pool-8982-thread-67:r.a.u.j.PerfRequestInterceptor - method=GET, uri==MDC1810C&items, response_time=1166, response_code=200 
4 console-2017_09_17.log:2017-09-17 00:04:02,655:INFO :pool-8984-thread-47:r.a.u.j.PerfRequestInterceptor - method=GET, uri==1031086&items, response_time=1005, response_code=200 
5 console-2017_09_17.log:2017-09-17 00:04:02,668:INFO :pool-8984-thread-50:r.a.u.j.PerfRequestInterceptor - method=GET, uri==F1018P&items, response_time=1018, response_code=200 
6 console-2017_09_17.log:2017-09-17 00:04:02,677:INFO :pool-8983-thread-86:r.a.u.j.PerfRequestInterceptor - method=GET, uri==V2581261&items, response_time=1060, response_code=200 
7 console-2017_09_17.log:2017-09-17 00:04:02,681:INFO :pool-8982-thread-68:r.a.u.j.PerfRequestInterceptor - method=GET, uri==202581&items, response_time=1196, response_code=200 
8 console-2017_09_17.log:2017-09-17 00:04:02,686:INFO :pool-8984-thread-46:r.a.u.j.PerfRequestInterceptor - method=GET, uri==1563200600&, response_time=1036, response_code=200 
9 console-2017_09_17.log:2017-09-17 00:04:02,693:INFO :pool-8983-thread-29:r.a.u.j.PerfRequestInterceptor - method=GET, uri==8DB355025371, response_time=7, response_code=200

更新2: 我是reduce参数uri。它可以包含不同数量的“=”。

答案1

sort它将以您指定的任何内容作为分隔符,因此只需指定正确的内容即可:

sort -n -t "=" -k5

第四位之后按数字排序=

答案2

因为没有什么阻碍,所以只需放弃-n以及-t选项:

sort -k 8 myfile.txt

但是如果你想知道为什么你的方法失败了,请让我们--debug

> sort -n -t " " -k 8 --debug myfile.txt
sort: using simple byte comparison
sort: key 1 is numeric and spans multiple fields
1 console-2017_09_17.log:2017-09-17 00:04:02,507:INFO :pool-8982-thread-30:r.a.u.j.PerfRequestInterceptor - method=GET, uri==43975&items, response_time=1030, response_code=300 
                                                                                                                                          ^ no match for key
________________________________________________________________________________________________________________________________________________________________________________
… … …

这表明你的有问题KEYDEF

KEYDEF 是 F[.C][OPTS][,F[.C][OPTS]],表示起始和终止位置,其中 F 是字段编号,C 是字段中的字符位置;两者均为原点 1,终止位置默认为行尾。如果 -t 和 -b 均无效,则字段中的字符从前一个空格的开头开始计算。OPTS 是一个或多个单字母排序选项 [bdfgiMhnRrV],它们会覆盖该键的全局排序选项。如果未指定键,则使用整行作为键。使用 --debug 诊断错误的键使用情况。

对于你的情况,这是正确的:

sort -n -t " " -k 8.15,8.19 myfile.txt

答案3

sort -k8V ex.txt

在哪里

  • -k8V意思是:key为第8个字段,排序条件为“版本排序”

版本排序是最自然的排序 - 对字母使用字母排序,但当出现数字时切换到数字排序(它适用于数字,单词和混合)

答案4

sort -t " " -k 8 myfile.txt将所需结果输出到终端。您不需要使用-n参数,因为您要排序的字段不只包含数字。

相关内容