I have this simple text file and want to sort all lines by the response_time
column:
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
I tried it this way:
sort -n -t " " -k 8 myfile.txt
But I still got an unsorted file. Why?
UPD: Time can be less than 1000 and more than 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
UPD2:
I'm reduce parameter uri
. It can contain different numbers of "=".
As there's nothing in the way just drop the
-n
as well as the-t
option:If you however want to know why your way failed, let's
--debug
:It shows there's something wrong with your
KEYDEF
:For your case this would be the right one:
sort
takes as a delimiter whatever you give it, so just give it the right thing:sort numerically after the fourth
=
.where
-k8V
means: key is the 8.th field, and order criteria is "version sort"Version sort is the most natural sort -- uses alphabetic sort for letters but switches to numeric sort when number appears (it works both for numbers, words, and mixed)
sort -t " " -k 8 myfile.txt
outputs to the terminal the desired result. You don't need to use-n
parameter because the field you want to sort by doesn't contain only digits.