ファイルの2つのタイムスタンプ行間のGrepとマージ行 - Unix

ファイルの2つのタイムスタンプ行間のGrepとマージ行 - Unix

Logfile.txtログファイルから特定のパターンの行をgrepし、出力を新しいテキストファイルにリダイレクトしようとしています1.txt。ログファイルには^Mいくつかの制御文字が含まれており、これらの行はタイムスタンプ行間で使用できるため、複数の行をすべて単一行にマージし、前の行にパターンがExecute含まれている場合は期待される結果に追加してみました。でも期待通りには得られない

ログファイル.txt

    1993-01-04 06:24:03,068 INFO  b: [Cool: read-189231]: Opening the File
    1993-01-04 06:24:13,068 INFO  b: [Cool: read-189231]: Checking the content
    1993-01-04 06:24:23,148 INFO  b: [Cool: read-189231]: Setting the session:
    1993-01-04 06:24:25,068 INFO  b: [Cool: read-189231]: Checking the content
    1993-01-04 06:24:25,068 INFO  b: [Cool: read-189231]: Compiling the query
    1993-01-04 06:24:27,148 INFO  ab: [Cool: read-189231]: Completed
    1993-01-04 06:25:22,168 INFO  ba: [Cool: read-190983]: Execute ^M
    I am just checking the answer ^M
    Need your support for the workaround ^M
    This log has some control character ^M
    1993-01-04 06:25:22,168 INFO  a: [Cool: read-190983]: Main Execution completed
    1993-01-04 06:25:52,188 INFO  ba: [Cool: read-190983]: Execute the line : How are you
    1993-01-04 06:26:45,268 INFO  a: [Cool: read-190983]: Exiting

コード - 私が見つけた結果

egrep -wah 'Setting the session:|Checking the line|Completed|Execute|Exiting' Logfile.txt > 1.txt

予想される結果

1993-01-04 06:24:23,148 INFO  b: [Cool: read-189231]: Setting the session:
1993-01-04 06:24:25,068 INFO  b: [Cool: read-189231]: Checking the line
1993-01-04 06:24:27,148 INFO  ab: [Cool: read-189231]: Completed
1993-01-04 06:25:22,168 INFO  ba: [Cool: read-190983]: Execute ^M I am just checking the answer ^M Need your support for the workaround ^M This log has some control character ^M
1993-01-04 06:25:52,188 INFO  ba: [Cool: read-190983]: Execute the line : How are you
1993-01-04 06:26:45,268 INFO  a: [Cool: read-190983]: Exiting

ベストアンサー1

努力する

awk '{X = $0; while (/\r$/) {getline; if (! /^[0-9]/) X = X $0}; gsub (/\r/, "", X); print X} 1 ' file

^M文字が表示されたら、日付を読むまでループを回してより多くの行を追加してから、行全体を印刷します。

おすすめ記事