Usando sort
e awk
Suponha que o formato da sua data seja o seguinte
08/03/2015
- > Dia, mês, ano
usando o comando abaixo.
- É um comando (observe o
\
no final das linhas) - Substitua
your_input_file
pelo seu nome de arquivo.
O comando
awk '{for (i=1;i<=NF;i++) {if (i==3) {printf "%s/%s/%s\t",substr($3,7,4),substr($3,4,2),substr($3,1,2)} else {printf "%s\t",$i}} printf "\n"}' your_input_file |\
sort -k3 -k4 |\
awk '{for (i=1;i<=NF;i++) {if (i==3) {printf "%s/%s/%s\t",substr($3,9,2),substr($3,6,2),substr($3,1,4)} else {printf "%s\t",$i}} printf "\n"}'
Discriminação
-
O primeiro comando
awk
corrige a data deday/month/year
08/03/2015
para
year/month/day
2015/03/08
-
O comando
sort
classifica a nova estrutura -
O segundo
awk
faz alterações no primeiroawk
desfazer
Se o formato de data não for day/month/year
, você terá que alterar a parte
-
substr($3,7,4),substr($3,4,2),substr($3,1,2)
e
-
substr($3,9,2),substr($3,6,2),substr($3,1,4)
Explicação
substr(field,start_position,length)
-
field
não muda, é a terceira coluna
-
start_position
,length
retorna uma substring de
start_position
com um comprimento delength
Exemplo
O arquivo de entrada foo
cat foo
090100010000481074 1 08/03/2015 09:35:17.934 LA150803000AJSX00000 LA150803000AJSX CRBP Buy ELF 100 1980000 119 3 2 1890000 119 100 2040000 119 100 1980000 119 1000 #Q 2
090100010000481074 1 07/03/2015 09:35:15.934 LA150803000AJSX00000 LA150803000AJSX CRBP Buy ELF 100 1980000 119 3 2 1890000 119 100 2040000 119 100 1980000 119 1000 #Q 2
090100010000481074 1 07/03/2015 09:35:17.934 LA150803000AJSX00000 LA150803000AJSX CRBP Buy ELF 100 1980000 119 3 2 1890000 119 100 2040000 119 100 1980000 119 1000 #Q 2
090100010000481074 1 08/03/2015 09:35:15.934 LA150803000AJSX00000 LA150803000AJSX CRBP Buy ELF 100 1980000 119 3 2 1890000 119 100 2040000 119 100 1980000 119 1000 #Q 2
090100010000481074 1 08/03/2016 09:35:17.934 LA150803000AJSX00000 LA150803000AJSX CRBP Buy ELF 100 1980000 119 3 2 1890000 119 100 2040000 119 100 1980000 119 1000 #Q 2
090100010000481074 1 03/07/2016 09:35:15.934 LA150803000AJSX00000 LA150803000AJSX CRBP Buy ELF 100 1980000 119 3 2 1890000 119 100 2040000 119 100 1980000 119 1000 #Q 2
090100010000481074 1 07/03/2016 09:35:17.934 LA150803000AJSX00000 LA150803000AJSX CRBP Buy ELF 100 1980000 119 3 2 1890000 119 100 2040000 119 100 1980000 119 1000 #Q 2
090100010000481074 1 08/03/2015 09:35:15.934 LA150803000AJSX00000 LA150803000AJSX CRBP Buy ELF 100 1980000 119 3 2 1890000 119 100 2040000 119 100 1980000 119 1000 #Q 2
A saída de
awk '{for (i=1;i<=NF;i++) {if (i==3) {printf "%s/%s/%s\t",substr($3,7,4),substr($3,4,2),substr($3,1,2)} else {printf "%s\t",$i}} printf "\n"}' foo | sort -k3 -k4 | awk '{for (i=1;i<=NF;i++) {if (i==3) {printf "%s/%s/%s\t",substr($3,9,2),substr($3,6,2),substr($3,1,4)} else {printf "%s\t",$i}} printf "\n"}'
090100010000481074 1 07/03/2015 09:35:15.934 LA150803000AJSX00000 LA150803000AJSX CRBP Buy ELF 100 1980000 119 3 2 1890000 119 100 2040000 119 100 1980000 119 1000 #Q 2
090100010000481074 1 07/03/2015 09:35:17.934 LA150803000AJSX00000 LA150803000AJSX CRBP Buy ELF 100 1980000 119 3 2 1890000 119 100 2040000 119 100 1980000 119 1000 #Q 2
090100010000481074 1 08/03/2015 09:35:15.934 LA150803000AJSX00000 LA150803000AJSX CRBP Buy ELF 100 1980000 119 3 2 1890000 119 100 2040000 119 100 1980000 119 1000 #Q 2
090100010000481074 1 08/03/2015 09:35:15.934 LA150803000AJSX00000 LA150803000AJSX CRBP Buy ELF 100 1980000 119 3 2 1890000 119 100 2040000 119 100 1980000 119 1000 #Q 2
090100010000481074 1 08/03/2015 09:35:17.934 LA150803000AJSX00000 LA150803000AJSX CRBP Buy ELF 100 1980000 119 3 2 1890000 119 100 2040000 119 100 1980000 119 1000 #Q 2
090100010000481074 1 07/03/2016 09:35:17.934 LA150803000AJSX00000 LA150803000AJSX CRBP Buy ELF 100 1980000 119 3 2 1890000 119 100 2040000 119 100 1980000 119 1000 #Q 2
090100010000481074 1 08/03/2016 09:35:17.934 LA150803000AJSX00000 LA150803000AJSX CRBP Buy ELF 100 1980000 119 3 2 1890000 119 100 2040000 119 100 1980000 119 1000 #Q 2
090100010000481074 1 03/07/2016 09:35:15.934 LA150803000AJSX00000 LA150803000AJSX CRBP Buy ELF 100 1980000 119 3 2 1890000 119 100 2040000 119 100 1980000 119 1000 #Q