Find/Replace in XML file

I am not sure how to approach this find/replace using a shell script. Any help or guidance appreciated.

I want to find this:

<objects/>
<thumb>thumb_0001.jpg</thumb>
<preview>preview_0001.jpg</preview>

And replace with something like this:

<objects>
<link>preview_0001.html</link>
</objects>
<thumb>thumb_0001.jpg</thumb>
<preview>preview_0001.jpg</preview>

I cant figure out how to make my replacement string include part of what i found, since i want the name of the html to be the name of the preview image. Is this possible with sed or awk?

something like:

#  nawk -F"[_.]" '!/thumb/{print} /thumb/{print "<link>preview_"$2".html</link>"; print "</objects>";print}' infile
<objects/>
<link>preview_0001.html</link>
</objects>
<thumb>thumb_0001.jpg</thumb>
<preview>preview_0001.jpg</preview>

what you're after ?

1 Like