in reply to How to make substitute greedy like sed's substitute

I also agree with Jenda. I regularly work with scanning for needles in 20 gigabyte XML dumps, where loading the data entirely into memory would be impossibly slow.

I recommend using the right tool for the job. Would you write a perl script to search through text files for lines matching a static pattern, or would you just use grep? The same is true for XML - would you write a script to search through XML when you could just use the XML equivalent of grep?

I put your XML snippet into a file called test.xml, and wrapped it with <root> </root>. Then I ran this command:

xml sel -t -c '*/Reasons' test.xml

This printed just the <Reasons> tags and whatever was inside them, i.e. what your code does. The xml command is a piece of software called XMLStarlet and it's designed to be the XML equivalent of grep/awk/sed. I'd recommend using it.