Find string in XML file, copy contents of section

I am new, really new to bash scripts.

I want to search an XML file for a certain string, say "1234567890"

Once found, I want to copy the entire contents from the previous instance of the string "Entity" to the next instance of "/Entity" to a txt file.

And then continue searching for the string in the same file.

Thank you for any guidance,

jrfiol

What's your system?

With some versions of awk/nawk you can do this:

$ echo "<entity><slartibartfast>1234567890</slartibartfast></entity><entity><slood>abcd</slood></entity>" |
        awk -v RS="<entity" -v FS="<" '/1234567890/ && ($NF == "/entity>") { print RS $0 }'
<entity><slartibartfast>1234567890</slartibartfast></entity>

$
1 Like

My system is RHEL 5.5

RHEL uses GNU awk, so Corona's example should work.

Thanks for the reply, i have been able to get the awk command to get the value i was looking for. thaks!