Para obter logs entre registros de data e hora específicos usando o shell script

2

Eu quero extrair infromation de logs usando um script de shell para um intervalo de tempo específico. Uma linha nos registros se parece com isso:

[14:58:27:176][15-10-2015]: testing1-vidya-Thu Oct 15 11:49:27 IST 2015
[14:59:27:178][15-10-2015]: testing2-vidya-Thu Oct 15 11:49:27 IST 2015
[15:00:27:179][15-10-2015]: testing3-vidya-Thu Oct 15 11:49:27 IST 2015
[15:01:27:180][15-10-2015]: testing4-vidya-Thu Oct 15 11:49:27 IST 2015
[15:01:27:181][15-10-2015]: testing5-vidya-Thu Oct 15 11:49:27 IST 2015
[15:02:27:182][15-10-2015]: testing6-vidya-Thu Oct 15 11:49:27 IST 2015
[15:02:27:183][15-10-2015]: testing7-vidya-Thu Oct 15 11:49:27 IST 2015
[15:02:27:184][15-10-2015]: testing8-vidya-Thu Oct 15 11:49:27 IST 2015
[15:02:27:191][15-10-2015]: testing9-vidya-Thu Oct 15 11:49:27 IST 2015
[15:03:27:192][15-10-2015]: testing10-vidya-Thu Oct 15 11:49:27 IST 2015
[15:03:27:193][15-10-2015]: testing11-vidya-Thu Oct 15 11:49:27 IST 2015
[15:03:27:208][15-10-2015]: testing12-vidya-Thu Oct 15 11:49:27 IST 2015
[15:04:27:209][15-10-2015]: testing13-vidya-Thu Oct 15 11:49:27 IST 2015
[15:49:27:210][15-10-2015]: testing14-vidya-Thu Oct 15 11:49:27 IST 2015
[13:02:17:209][15-10-2015]: testing13-vidya-Thu Oct 15 11:49:27 IST 2015
[15:04:27:209][16-10-2015]: testing13-vidya-Fri Oct 16 11:49:27 IST 2015
[15:49:27:210][16-10-2015]: testing14-vidya-Fri Oct 16 11:49:27 IST 2015

Eu preciso extrair dados em intervalos específicos. por exemplo entre 14:59 e 15:03 em 15-10-2015 como mostrado abaixo:

[14:59:27:178][15-10-2015]: testing2-vidya-Thu Oct 15 11:49:27 IST 2015
[15:00:27:179][15-10-2015]: testing3-vidya-Thu Oct 15 11:49:27 IST 2015
[15:01:27:180][15-10-2015]: testing4-vidya-Thu Oct 15 11:49:27 IST 2015
[15:01:27:181][15-10-2015]: testing5-vidya-Thu Oct 15 11:49:27 IST 2015
[15:02:27:182][15-10-2015]: testing6-vidya-Thu Oct 15 11:49:27 IST 2015
[15:02:27:183][15-10-2015]: testing7-vidya-Thu Oct 15 11:49:27 IST 2015
[15:02:27:184][15-10-2015]: testing8-vidya-Thu Oct 15 11:49:27 IST 2015
[15:02:27:191][15-10-2015]: testing9-vidya-Thu Oct 15 11:49:27 IST 2015
[15:03:27:192][15-10-2015]: testing10-vidya-Thu Oct 15 11:49:27 IST 2015
[15:03:27:193][15-10-2015]: testing11-vidya-Thu Oct 15 11:49:27 IST 2015
[15:03:27:208][15-10-2015]: testing12-vidya-Thu Oct 15 11:49:27 IST 2015

Eu sou novo no shell script.Eu tentei com o comando grep. Alguém pode me sugerir como fazer isso?

    
por Vidya 15.10.2015 / 16:11

2 respostas

4

Você pode usar sed .

sed -n '/\[14:59/,/\[15:03/p' log

Atualização: remota via ssh

ssh [email protected] "sed -n -e '/\[14:59/,/\[15:03/p' /var/logfile"

Para extrair apenas linhas que foram encontradas entre os tempos de interesse em uma data específica, inclua a data em sua string de pesquisa:

sed -n '/\[14:59.*15-10-2015*/,/\[15:03.*15-10-2015/p' log
    
por 15.10.2015 / 16:20
2

Grep, que pode usar expressões regulares, como esta:

grep -E "^\[(14:59|15:00|15:01|15:02|15:03)" /path/to/file
    
por 15.10.2015 / 16:20

Tags