大容量ログファイルでのテキストの検索とフィルタリング

大容量ログファイルでのテキストの検索とフィルタリング

tail、head、grepコマンドを使用してログファイルを取得しました。ほとんどの場合、パイプを使用することに加えて、これら3つのコマンドを組み合わせて作業を完了します。ただし、多くのデバイスでほぼ数秒ごとに報告するログがあります。したがって、このログは非常に大きいです。ただし、報告されたパターンは同じです。

Oct 10 11:58:50 Received Packet from [xxx.xx.xxx.xx:xxxx]: 0xD 0xD 0xD 
Oct 10 11:58:50 Unit ID: 1111

上記の例は、特定のユニットIDのソケットサーバーにUDPパケットが送信されたことを示しています。

今度は、ログを照会して特定の時間範囲内でこのコンピュータのパケット情報を確認したい場合があります。

Oct 10 11:58:50 Received Packet from [xxx.xx.xxx.xx:xxxx]: 0xD 0xD 0xD 
Oct 10 11:58:50 Unit ID: 1111

... // A bunch of other units reporting including unit id 1111

Oct 10 23:58:50 Received Packet from [xxx.xx.xxx.xx:xxxx]: 0x28 0x28 0x28 
Oct 10 23:58:50 Unit ID: 1111

したがって、上記の例では、11:58と23:58の時間範囲でUnit ID:1111のログ出力のみを表示しようとしています。したがって、考えられる結果は次のとおりです。

Oct 10 11:58:50 Received Packet from [xxx.xx.xxx.xx:xxxx]: 0xD 0xD 0xD 
Oct 10 11:58:50 Unit ID: 1111

Oct 10 12:55:11 Received Packet from [xxx.xx.xxx.xx:xxxx]: 0x28 0xD 0x28 
Oct 10 12:55:11 Unit ID: 1111

Oct 10 15:33:50 Received Packet from [xxx.xx.xxx.xx:xxxx]: 0x33 0xD 0x11 
Oct 10 15:33:50 Unit ID: 1111

Oct 10 23:58:50 Received Packet from [xxx.xx.xxx.xx:xxxx]: 0x28 0x28 0x28 
Oct 10 23:58:50 Unit ID: 1111

結果にはデバイスID:1111に関する情報のみが表示され、他のデバイスには表示されません。

今、このようなものを使用するときの問題は次のとおりです。

tail -n 10000 | grep -B20 -A20 "Oct 10 23:58:50 Unit ID: 1111" 

はい、私に必要なものだけでなく、たくさんのものを見せてくれます。

ベストアンサー1

awk '$3 >= "11:58" && $3 <= "23:58" && /Unit ID: 1111/{print l"\n"$0};{l=$0}'

おすすめ記事