Lendo logs com a palavra 'ERROR' entre 2 datas

2

Os registros estão no formato:

2018-06-25 00:00:20,073 DEBUG SAMPLE TEXT CONTENT
2018-06-26 00:00:20,073 ERROR SAMPLE TEXT CONTENT
2018-06-26 17:37:20,073 ERROR SAMPLE TEXT CONTENT
2018-06-26 19:00:20,073 DEBUG SAMPLE TEXT CONTENT
2018-06-27 00:00:20,073 DEBUG SAMPLE TEXT CONTENT
2018-06-28 00:00:20,073 ERROR SAMPLE TEXT CONTENT
2018-06-29 00:00:20,073 ERROR SAMPLE TEXT CONTENT
2018-06-30 00:00:20,073 DEBUG SAMPLE TEXT CONTENT
2018-07-01 11:00:20,073 ERROR SAMPLE TEXT CONTENT
2018-07-02 02:00:20,073 DEBUG SAMPLE TEXT CONTENT
2018-07-02 00:00:20,073 ERROR SAMPLE TEXT CONTENT
2018-07-03 20:00:20,073 ERROR SAMPLE TEXT CONTENT

Eu quero encontrar e exibir todos os logs entre duas datas (suponha 2018-06-26 a 2018-07-02) que tenham a palavra ERROR nela. Eu vou tirar as datas dinamicamente para que sejam variáveis $ FROM e $ TO.

Qualquer implementação grep , awk ou sed fará

EDITAR: As datas $ TO e $ FROM devem ser inclusivas e podem precisar acessar vários arquivos de log para que os logs não sejam classificados

    
por Pratik Mayekar 25.06.2018 / 14:26

4 respostas

3

awk, usando a opção -v para passar as variáveis do shell para as variáveis do awk:

$ awk -v from="$FROM" -v to="$TO" 'from <= $1 && $1 <= to && /ERROR/' log.txt
2018-06-26 00:00:20,073 ERROR SAMPLE TEXT CONTENT
2018-06-26 17:37:20,073 ERROR SAMPLE TEXT CONTENT
2018-06-28 00:00:20,073 ERROR SAMPLE TEXT CONTENT
2018-06-29 00:00:20,073 ERROR SAMPLE TEXT CONTENT
2018-07-01 11:00:20,073 ERROR SAMPLE TEXT CONTENT
2018-07-02 00:00:20,073 ERROR SAMPLE TEXT CONTENT
    
por 25.06.2018 / 16:50
1

Usando o AWK:

awk '/'$FROM'/,/'$TO'/ {print $0}' log.txt | grep ERROR

Usando sed:

sed -n '/'$FROM'/,/'$TO'/p' log.txt | grep ERROR
    
por 25.06.2018 / 14:37
1

Simples, mas eficaz;

egrep '2018-06-26|2018-07-02' log file | grep "ERROR"

    
por 25.06.2018 / 18:17
0

Repita as datas ( via ):

FROM=2018-06-26
TO=2018-07-02
d=$FROM
while [ $d != $TO ]; do 
  grep "^${d}.*ERROR" logfile
  d=$(date -I -d "$d + 1 day")
done
    
por 25.06.2018 / 14:39

Tags