Say I have the below simplified type of XML file and need to extract all of the string data that is within the <innerElement>
and </innerElement>
tags only for the Id 1234.
<outerTag>
<innerElement>
<Id>1234</Id>
<fName>Kim</fName>
<lName>Scott</lName>
<customData1>Value1</customData1>
<customData2>Value2</customData2>
<position>North</position>
<title/>
</innerElement>
<innerElement>
<Id>5678</Id>
<fName>Brian</fName>
<lName>Davis</lName>
<customData3>value3</customData3>
<customData4>value4</customData4>
<customData5>value5</customData5>
<position>South</position>
<title/>
</innerElement>
</outerTag>
My expected output is:
<innerElement>
<Id>1234</Id>
<fName>Kim</fName>
<lName>Scott</lName>
<customData1>Value1</customData1>
<customData2>Value2</customData2>
<position>North</position>
<title/>
</innerElement>
Using what I have read on other posts I have tried using grep -z to match multiline strings (treating the file contents as a single line) and -o to print only exact matches, but when I use the .* wildcard after the Id element it ends matching everything up to the end of the file instead of stopping on the fist occurrence.
grep -zo '<innerElement>.*<Id>1234</Id>.*</innerElement>' myfile.xml
How can I make the pattern match up to only the fist occurrence or the tag after Id 1234?