マルチパターンマッチングとシングルライン印刷

マルチパターンマッチングとシングルライン印刷

ログファイルで両方のパターンを一致させ(両方のパターンで)、一致するパターンの次の行を取得し、最後にこれら3つの値を1行に印刷する必要があります。

サンプルログファイル:

2013/09/05 04:26:00          Processing Batch /fbc/dev/cebi/dod/9739867262
2013/09/05 04:26:02          Batch 9739867262 was successful
2013/09/05 04:26:02          Total Time          =  3.13 Secs
2013/09/05 04:26:02          Repository API Time =  2.96 Secs
2013/09/05 04:26:02          File System Io Time =  0.06 Secs
2013/09/05 04:26:02          Doc Validation Time =  0.03 Secs
2013/09/05 04:26:02      Ending @ Thu Sep 05 04:26:02 EDT 2013
2013/09/05 08:18:10      Starting @ Thu Sep 05 08:18:10 EDT 2013
2013/09/05 08:18:10      Starting @ Thu Sep 05 08:18:10 EDT 2013
2013/09/05 08:18:10          Processing Batch /fbc/dev/cebi/dod/9844867675
2013/09/05 08:18:10          Processing Batch /fbc/dev/cebi/dod/9886743777
2013/09/05 08:18:16          Batch 9844867675 was successful
2013/09/05 08:18:16          Total Time          =  6.00 Secs
2013/09/05 08:18:16          Repository API Time =  5.63 Secs
2013/09/05 08:18:16          File System Io Time =  0.05 Secs
2013/09/05 08:18:16          Doc Validation Time =  0.19 Secs
2013/09/05 08:18:16      Ending @ Thu Sep 05 08:18:16 EDT 2013
2013/09/05 08:18:18          Batch 9886743777 was successful
2013/09/05 08:18:18          Total Time          =  8.27 Secs
2013/09/05 08:18:18          Repository API Time =  8.52 Secs
2013/09/05 08:18:18          File System Io Time =  0.08 Secs
2013/09/05 08:18:18          Doc Validation Time =  0.47 Secs
2013/09/05 08:18:18      Ending @ Thu Sep 05 08:18:18 EDT 2013

cust_no.txtというファイルに数字を別々に入れました。

9739867262
9844867675
9886743777

この数値を入力として使用して、ログファイル内の次の2つのパターンを一致させる必要があります。

  1. プロセスの配置 /fbc/dev/cebi/dod/
  2. 一括成功

出力には以下が必要です。

->最初のパターン()一致でi.e Processing Batch /fbc/dev/cebi/dod/<numbers in the cust_no.txt>2番目の単語$ 2を取得する必要があります。 -> 2番目のパターン()一致で2番目の単語、i.e Batch <numbers in the cust_no.txt> was successful$ 2を取得する必要があります。 -> 2番目のパターンの後、一致の後、次の行の6番目の単語($ 6)(つまり、で始まる行Total Time)を取得する必要があります。

希望の出力:

9739867262,04:26:00,04:26:02,3.13 Secs
9844867675,08:18:10,08:18:16,6.00 Secs
9886743777,08:18:10,08:18:18,8.27 Secs

これを得るために次のことを試しましたが、うまくいかないようです。

awk -v cn=$cust_no '{{if ($0 ~ "Processing.*" cn) st=$2 && if ($0 ~ "Customer cn was successful" et=$2; getline; tt=$4} ; print st,et,tt}

ベストアンサー1

これはどうですか:

while read number;do
    start=$(grep "Processing Batch /fbc/dev/cebi/dod/$number" log_file\
            |head -n 1|awk '{print $2}')
    end=$(grep -A 1 "Batch $number was successful" log_file\
            |head -n 2|tail -n 1|awk -v OFS=',' '{print $2,$6}')
    echo "$number,$start,$end Secs"
done <cust_no.txt

おすすめ記事