ログファイルを追跡して把握して1行にまとめ、別のコマンドに引数として渡す方法

ログファイルを追跡して把握して1行にまとめ、別のコマンドに引数として渡す方法

次のコマンドを使用して、以下のエラーメッセージ(合計3行)を取得しました。

$ tail -f -n 0 error.log | grep -A2 --line-buffered "Internal server error"
! @79884flo2 - Internal server error, for (GET) [/] ->

play.api.UnexpectedException: Unexpected exception[Missing: No configuration setting found for key init.default]

ログを1行にまとめるいくつかのスキルを試して成功しました。

  1. xargs

    tail -f -n 0 error.log | grep -A2 --line-buffered "error" | xargs -I @ printf "%s" "@"
    
  2. awk

    tail -f -n 0 error.log | grep -A2 --line-buffered "error" | awk '{ printf("%s ", $0); }'
    
  3. paste:

    tail -f -n 0 error.log | grep -A2 --line-buffered "error" | paste -d " " - - -
    

telegram-notify次に、次のコマンドを使用して出力をテレグラムに送信したいと思います。

telegram-notify --error --title "<title>" --text "<output from above command goes here>"

現在私ができる最善は、xargsテレグラムを使用してログの各行を個別に送信することです。

tail -f -n 0 error.log | grep -A2 --line-buffered "error" | xargs -L 3 -I {} telegram-notify --error --title "<title>" --text "{}"

アドバイスしてください:

  1. 上記で試した3つのxargsコマンドのうち、awkどのコマンドをpaste使用して行を結合する必要がありますか?

  2. xargs//でコマンドオプションで出力をさらにパイプまたは渡す方法awkpastetelegram-notify--text

ベストアンサー1

おすすめ記事