I have a log file on unix host (quite big one >500 MB) on daily basis with printed out XMLs there that are sent to other systems.
The XML structure is likely to the following one:
2021-01-04 08:44:43,174 Not related message
2021-01-04 08:45:45,174 [Sender] INFO com....router.MessageRouter - Publishing Message: <?xml version="1.0" encoding="UTF-8"?>
<Q1:book>
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
<id>6AA2BC</id>
<metadata>
<isbn>123-432-234</isbn>
<catalogId>111</catalogId>
</metadata>
<relatedBook>
<metabook id="3AA345">Everyday Italian. Part 2</metabook>
<metabook id="7ANNN2">Everyday Italian. Part 3</metabook>
</relatedBook>
</Q1:book>
2021-01-04 08:46:23,174 Not related message
2021-01-04 08:46:51,174 Not related message
2021-01-04 08:47:21,174 Not related message
2021-01-04 08:49:45,174 Message: <?xml version="1.0" encoding="UTF-8"?>
<Q1:book>
<title lang="en">Everyday Italian. Part 2</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
<id>3AA345</id>
<metadata>
<isbn>123-432-235</isbn>
<catalogId>115</catalogId>
</metadata>
<relatedBook>
<metabook id="6AA2BC">Everyday Italian. Part 2</metabook>
<metabook id="7ANNN2">Everyday Italian. Part 3</metabook>
</relatedBook>
</Q1:book>
and there are dozens of XMLs in log like that.
I have id of the my desired xml (e.g. 6AA2BC) and I need to extract the full XML related to it using Linux commands.
if I do something like grep 6AA2BC file.log
I will receive the only line <id>3AA345</id>
I have tried different approaches described by community and was able to get ALL XMLs using
grep -Poz '(?<=<Q1:book)(.*?\n)*?.*?(?=</Q1:book>)' file.log
but if I add id to the request, it does not work
The expected output: full XML with that particular id=6AA2BC
<Q1:book>
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
<id>6AA2BC</id>
<metadata>
<isbn>123-432-234</isbn>
<catalogId>111</catalogId>
</metadata>
<relatedBook>
<metabook id="3AA345">Everyday Italian. Part 2</metabook>
<metabook id="7ANNN2">Everyday Italian. Part 3</metabook>
</relatedBook>
</Q1:book>
Can anyone, please, explain what's the best way to do this?