Use o awk para encontrar a primeira ocorrência

2

Eu tenho um arquivo de log que é atualizado quando meu script é executado. O script inserirá o texto "Início do script" quando ele for iniciado e o texto "Fim do script" quando ele terminar a execução. Eu estou tentando capturar o texto entre o "Início do script" e "Fim do script". As entradas mais recentes estão na parte inferior do log.

Usar o seguinte está próximo, mas está me dando todas as ocorrências no log, como mostrado abaixo.

tac /opt/novell/JDBCFanout/activemqstatus.log |awk '/End of script/,/Start of script/'|tac

2014-09-09 12:30:42 - Start of script
2014-09-09 12:30:42 - Monitoring Reset script for the ActiveMQ.
2014-09-09 12:30:42 - The ActiveMQ value is not 1, the ActiveMQ services will not be restarted. The current value is 0.
2014-09-09 12:31:35 - The Fanout driver state is:  0
2014-09-09 12:33:32 - Sleeping for 10 seconds before checking the status of the Fanout driver.
2014-09-09 12:35:05 - The Fanout driver state is:  1
2014-09-09 12:35:05 - ERROR: The Fanout driver failed to start. The Fanout driver needs to be manually restarted.
2014-09-09 12:35:05 - End of script
2014-09-09 13:17:17 - Start of script
2014-09-09 13:17:17 - Reset script for the ActiveMQ.
2014-09-09 13:17:17 - The ActiveMQ flag is 1, shutting down the ActiveMQ services and the Fanout driver.
2014-09-09 13:17:17 - The ActiveMQ flag is now set to 0.
2014-09-09 13:17:17 - Stopping the Fanout driver.
2014-09-09 13:17:27 - The script is now cleaning up the pid's.
2014-09-09 13:17:37 - The script is now archiving the ActiveMQ Logs.
2014-09-09 13:17:37 - No files older than 60 days.
2014-09-09 13:17:47 - The script is now starting the ActiveMQ services.
2014-09-09 13:19:57 - The ActiveMQ service is running,
2014-09-09 13:19:57 - The ActiveMQ Oracle service is running.
2014-09-09 13:19:57 - The ActiveMQ MSSQL service is running.
2014-09-09 13:19:57 - The ActiveMQ Queue Manager service is running.
2014-09-09 13:19:58 - Sleeping for 10 seconds before checking the status of the Fanout driver.
2014-09-09 13:20:09 - The Fanout driver successfully restarted.
2014-09-09 13:20:09 - End of script

Especificamente, gostaria que a saída fosse assim e não todas as ocorrências, como mostrado acima.

2014-09-09 13:17:17 - Start of script
2014-09-09 13:17:17 - Reset script for the ActiveMQ.
2014-09-09 13:17:17 - The ActiveMQ flag is 1, shutting down the ActiveMQ services and the Fanout driver.
2014-09-09 13:17:17 - The ActiveMQ flag is now set to 0.
2014-09-09 13:17:17 - Stopping the Fanout driver.
2014-09-09 13:17:27 - The script is now cleaning up the pid's.
2014-09-09 13:17:37 - The script is now archiving the ActiveMQ Logs.
2014-09-09 13:17:37 - No files older than 60 days.
2014-09-09 13:17:47 - The script is now starting the ActiveMQ services.
2014-09-09 13:19:57 - The ActiveMQ service is running,
2014-09-09 13:19:57 - The ActiveMQ Oracle service is running.
2014-09-09 13:19:57 - The ActiveMQ MSSQL service is running.
2014-09-09 13:19:57 - The ActiveMQ Queue Manager service is running.
2014-09-09 13:19:58 - Sleeping for 10 seconds before checking the status of the Fanout driver.
2014-09-09 13:20:09 - The Fanout driver successfully restarted.
2014-09-09 13:20:09 - End of script

Agradeço antecipadamente por qualquer ajuda que você possa compartilhar!

    
por apropos 09.09.2014 / 20:44

2 respostas

3

Talvez uma pequena máquina de estado:

tac file |
awk '/End of script/ {p=1} p {print} p && /Start of script/ {exit}' |
tac
    
por 09.09.2014 / 20:58
1

Indiscutivelmente mais simples que a resposta de glenn (embora seja necessário digitar “Start of script” duas vezes):

tac logfile | awk '/End of script/,/Start of script/{print} /Start of script/{exit}' | tac

ou

tac logfile | sed -n '/End of script/,/Start of script/p; /Start of script/q' | tac
    
por 09.09.2014 / 21:18