Cut string between two XML nodes using GREP

Say you want to extract some string between two patterns. GREP’s Perl integration does a nice job to perform such match.

For example, the string given is,

<toplevel><suggestion data="hello kitty"/></toplevel>

And you want to extract between toplevel tags. The following GREP command do it nicely.

grep -P -o '(?<=(<tag>)).*(?=(</tag>))'

The whole script is

#!/bin/bash

# STRING = <toplevel><suggestion data="hello kitty"/></toplevel>
# OUTPUT = <suggestion data="hello kitty"/>

RESPONSE_STR='<toplevel><suggestion data="hello kitty"/></toplevel>'

echo "$RESPONSE_STR" | grep -P -o '(?<=(<toplevel>)).*(?=(</toplevel>))'