Usando o AWK: como definir campos de impressão para pesquisar linhas contendo datas que variam entre hoje e 5 dias atrás?

1

Estou tentando resumir essa lista para conter apenas cinco dias de dados, de hoje até cinco dias atrás. Eu tentei usar awk , mas sem sucesso. Não há necessidade de se ater ao awk também. Esta é a saída:

Este é o meu código awk para pesquisar por today's date em uma linha:

awk -F " " -v todate="$(date +%Y-%m-%d)" 'todate' output.txt

Este é o meu código de loop while:

while read line
    do
    if (("$(date +%Y-%m-%d)" in $line))
        then
            echo $line
done < output.txt

Este é o arquivo output.txt :

2018-05-09 14:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-10 03:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-10 16:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-11 05:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-11 18:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-12 07:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-12 20:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-13 09:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-13 22:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-14 11:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-15 00:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-15 08:00:00 UTC+0800;swlt;;BKP_USGSN03_OLP_MK8;CXS101289_R73B13(001-00-12)
2018-05-15 08:00:00 UTC+0800;sau;;2025,licensed_sau_normal;CXS101289_R73B13(001-00-12)
2018-05-15 08:00:00 UTC+0800;sau_lte;;1025,licensed_sau_lte_normal;CXS101289_R73B13(001-00-12)
2018-05-15 08:00:00 UTC+0800;pdp;;2001,licensed_pdp_normal;CXS101289_R73B13(001-00-12)
2018-05-15 13:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-16 02:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-16 15:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-17 04:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-17 17:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-18 06:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-18 19:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 08:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 21:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 10:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 23:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-21 12:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 01:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 14:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-23 03:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
    
por tthhss 23.05.2018 / 09:54

3 respostas

1

No script a seguir, a função mktime cria um registro de data e hora unix com base no primeiro e no segundo campo do arquivo que pode ser comparado com a data de referência de date fornecida no argumento para awk :

awk -v startdate=$(date -d '5 days ago' +%s) '{d=$1 OFS $2; gsub("[-:]", " ", d); t=mktime(d)} t>startdate' file
    
por 23.05.2018 / 10:10
1

Abordagem do awk do GNU:

awk -F'[:-]' -v from_date=$(date -d'-4 days' +%s) \
'mktime(sprintf("%d %d %d %d %d %d", $1, $2, $3, $4, $5, $6)) >= from_date' file.txt

Exemplo de saída:

2018-05-18 06:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-18 19:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 08:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 21:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 10:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 23:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-21 12:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 01:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 14:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-23 03:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
    
por 23.05.2018 / 10:11
0

Usando o AWK:

OPÇÃO 1:

DATE='date --date='5 days ago' +"%Y-%m-%d"';awk '/'$DATE'/,EOF { print $0 }' file.txt

OPÇÃO 2:

DATE='date +%Y-%m-%d -d "5 day ago"';awk '/'$DATE'/,EOF { print $0 }' file.txt

OPÇÃO 3:

awk '/2018-05-18/,EOF { print $0 }' file.txt

Saída:

2018-05-18 06:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-18 19:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 08:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-19 21:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 10:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-20 23:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-21 12:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 01:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-22 14:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
2018-05-23 03:40:22 UTC+0800;sau;;less_than_100,heartbeat;CXS101289_R73B13(001-00-12)
    
por 23.05.2018 / 10:15

Tags