Sherry Sherry - 2 months ago 20
Linux Question

Filtering a Block

I have multiple blocks of the below pattern

<APPLIANCE>
<ID>12233</ID>
<UUID>xxxx-xxxx-xxxx-xxxx-xxxxxxx</UUID>
<NAME>xxxxxxx</NAME>
<STATUS>Offline</STATUS>
</APPLIANCE>
<APPLIANCE>
<ID>12234</ID>
<UUID>xxxx-xxxx-xxxx-xxxx-xxxxxxx</UUID>
<NAME>yyyyy</NAME>
<STATUS>Offline</STATUS>
</APPLIANCE>


I want to extract a block with Particular ID and Particular Name.
The output should display

For example :-

<ID>12234</ID>
<NAME>yyyyy</NAME>


I wanted to do using grep, sed, awk

Thanks.

Answer

This sed should work for you:

sed -n '/<ID>12234/,/<NAME>/{//p}' file

But you'd better use an xml parser as xmllint or xmlstarlet to parse valid xml files.