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

おすすめ記事