I can see it working with "Second" hard coded - However, if i have it passed as a variable, I am not getting the results back
[b]Tried these -
pattern="Second";
sed -ne '/<termType>$pattern/{x;1!p;}' -e h record.xml
sed -ne '/<termType>"$pattern"/{x;1!p;}' -e h record.xml
sed -ne '/<termType>{$pattern}/{x;1!p;}' -e h record.xml
You can use the grep (or egrep) -B argument to to look one line above the search string and only search for <termType>Second". The downside is that you get the "<termType>Second</termType>" line and you didn't specify whether that's ok or not.
Anytime you start embedding values into a regular expression, you need to keep in mind that the regular expression will break if special characters are present and unescaped. In cases like these, if possible, it's best to use simple string comparisons when dealing with such values (k==$2 in the code that follows).
Thanks a ton here.. both above solutions work here - Thanks to alister and fubaya - You are awesome - appreciate feedback from other contributors on this thread
pattern="Second";
sed -ne '/<termType>$pattern/{x;1!p;}' -e h record.xml
sed -ne '/<termType>"$pattern"/{x;1!p;}' -e h record.xml
sed -ne '/<termType>{$pattern}/{x;1!p;}' -e h record.xml
You could have overcome this by using like this:
sed -ne '/<termType>'$pattern'/{x;1!p;}' -e h record.xml