Issue
I have a log file in this format.
MATNR -- 0000000000090933887
Some text
in
between
*****Return Message** Successfully processed
some
more
text
blah
blah
-----------------------------------------
MATNR-000000000000090934206
some
text
in
between
*****Return Message** Successfully processed
some more text
blah
blah
between
--------------------------------------------
This is how my log file is structured. I plan to fetch the MATNR line and the message on the line **** Return Message***. In the log each new entry is delimited by ------------.Is there a way using grep,sed,tcl program which can efficiently perform this action for us.
I tried attempting this by using grep using z option. But i am still not sure how i can attempt to fetch matching lines within those.Below just gives the MATNR which is surrounded by some lines.
grep -zPo '(\n-+\n)\K(.|\n)+?MATNR(.|\n)+?(?=\n-+\n)'
Expected output is
MATNR -- 0000000000090933887
*****Return Message** Successfully processed
MATNR-000000000000090934206
*****Return Message** Successfully processed
Solution
This awk
should do the job:
awk '/^MATNR/ {m=$0} /Return Message/ {print m, $0}' file
MATNR -- 0000000000090933887 *****Return Message** Successfully processed
MATNR-000000000000090934206 *****Return Message** Successfully processed
Answered By - anubhava