sort -t " " -k 8 myfile.txt
envia ao terminal o resultado desejado. Você não precisa usar o parâmetro -n
porque o campo que você deseja classificar não contém apenas dígitos.
Eu tenho este arquivo de texto simples e quero classificar todas as linhas pela coluna 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
Eu tentei assim:
sort -n -t " " -k 8 myfile.txt
Mas ainda tenho um arquivo não classificado. Por quê?
UPD: O tempo pode ser menor que 1000 e maior que 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:
Estou reduzindo o parâmetro uri
. Pode conter números diferentes de "=".
sort -t " " -k 8 myfile.txt
envia ao terminal o resultado desejado. Você não precisa usar o parâmetro -n
porque o campo que você deseja classificar não contém apenas dígitos.
sort
tem como delimitador o que quer que você dê, então apenas dê a coisa certa:
sort -n -t "=" -k5
classifica numericamente após o quarto =
.
Como não há nada no caminho, basta soltar a -n
e a opção -t
:
sort -k 8 myfile.txt
Se, no entanto, você quiser saber por que seu caminho falhou, vamos --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
________________________________________________________________________________________________________________________________________________________________________________
… … …
Ele mostra que há algo errado com seu KEYDEF
:
KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a field number and C a character position in the field; both are origin 1, and the stop position defaults to the line's end. If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace. OPTS is one or more single-letter ordering options [bdfgiMhnRrV], which override global ordering options for that key. If no key is given, use the entire line as the key. Use --debug to diagnose incorrect key usage.
Para o seu caso, este seria o caminho certo:
sort -n -t " " -k 8.15,8.19 myfile.txt
sort -k8V ex.txt
onde
-k8V
significa: chave é o 8.º campo e o critério de ordem é "classificação de versão" Classificação de versão é a classificação mais natural - usa classificação alfabética para letras, mas alterna para classificação numérica quando o número aparece (funciona para números, palavras e misto)