複数のパターンを一致させた後、awkまたはsedを使用してファイルから結果を分離します。

複数のパターンを一致させた後、awkまたはsedを使用してファイルから結果を分離します。

多くのINFOメッセージとシステム出力行を含むアプリケーションログの1つから2つのパターンに含まれる行を抽出し、結果行の他の行から具体的に値を取得できるようにしたいです。

これは私の入力ファイルです。

2019-08-16 00:38:29,171 1065142892 [http-bio-8443-exec-146] INFO aaaaa
2019-08-16 00:38:29,172 1065142893 [http-bio-8443-exec-146] INFO bbbbb
              'This is the matching pattern'
tag1: value1
tag2: value2
tag3: value3
'this is the end pattern' xxxyyyzzz

2019-08-16 00:39:29,171 1065142992 [http-bio-8443-exec-146] INFO aaaaa
2019-08-16 00:39:29,172 1065142993 [http-bio-8443-exec-146] INFO bbbbb
              'This is the matching pattern'
tag1: valuea
tag2: valueb
tag3: valuec
'this is the end pattern' xxxyyyzzzadasd

2019-08-16 00:38:29,171 1065142892 [http-bio-8443-exec-146] INFO aaaaa
2019-08-16 00:38:29,172 1065142893 [http-bio-8443-exec-146] INFO bbbbb
              'This is the matching pattern'
tag1: valuep

2019-08-16 01:38:29,171 1065153992 [http-bio-8443-exec-146] INFO aaaaa
2019-08-16 01:38:29,172 1065153993 [http-bio-8443-exec-146] INFO bbbbb
              'This is the matching pattern1'
tag1: valuexx
tag2: valueyy
tag3: valuezz
'this is the end pattern' xxxyyyzzzadasdqwerty

ここでは、次のように出力を抽出したいと思います。

出力:

value1, value2
valuea, valueb
valuexx, valueyy

結果をフィルタリングするには、次のように試しました。

awk '/This is the matching pattern/,/This is the end pattern/' logfile
OR
awk ' /This is the matching pattern/{flag=1;next}/This is the end pattern/{flag=0}flag' logfile
OR
sed -n -e '/This is the matching pattern/,/this is the end pattern/{ /This is the matching pattern/d; /this is the end pattern/d; p; }'  logfile

しかし、これらはラベル1:値p出力に一致する終了パターンの開始がありません。

ベストアンサー1

あなたの例ではテストする理由はなく、This is the matching pattern最後に正規表現しかありません。

$ cat tst.awk
BEGIN { FS=": "; OFS=", " }
{ f[$1] = $2 }
/this is the end pattern/ {
    print f["tag1"], f["tag2"]
    delete f
}

$ awk -f tst.awk file
value1, value2
valuea, valueb
valuexx, valueyy

おすすめ記事