使用 awk 查找第一次出现的位置

使用 awk 查找第一次出现的位置

我有一个日志文件,当我的脚本运行时该文件会更新。该脚本将在启动时插入“脚本开始”文本,并在运行结束时插入“脚本结束”文本。我试图捕获“脚本开始”和“脚本结束”之间的文本。最新条目位于日志底部。

使用以下内容很接近,但它给了我日志中的所有事件,如下所示。

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

具体来说,我希望输出看起来像这样,而不是上面显示的所有发生的情况。

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

预先感谢您可以分享的任何帮助!

答案1

也许是一个小状态机:

tac file |
awk '/End of script/ {p=1} p {print} p && /Start of script/ {exit}' |
tac

答案2

可以说比格伦的答案更简单(尽管它确实需要输入“脚本开始”两次):

塔克日志档案| awk '/脚本结束/,/脚本开始/{print} /脚本开始/{exit}' |塔克

或者

塔克日志档案| sed -n '/脚本结束/,/脚本开始/p; /脚本开始/q' |塔克

相关内容