I am trying to find a way to insert a string ' .sh' into a particular line in a text file using sed. The problem i have is that i only want to insert the string if a previous regex matches, it also has to support a variable number of lines between the 'name' and 'extension' tag and to be idempotent, so i can run it multiple times with only a single insertion of ' .sh' for the 'extension' tagged space separated list.
Here is a small snippet of the text file:-
<name>gba</name>
<fullname>Game Boy Advance</fullname>
<manufacturer>Nintendo</manufacturer>
<release>2001</release>
<hardware>portable</hardware>
<path>/storage/roms/gba</path>
<extension>.gba .GBA .zip .ZIP .7z .7Z</extension>
so i want to change <extension>.gba .GBA .zip .ZIP .7z .7Z</extension> to <extension>.gba .GBA .zip .ZIP .7z .7Z .sh</extension> ONLY if the name tag is <name>gba</name>
This is the best i have come up with so far, but it has two problems, firstly subsequent executions of the code duplicate the insertion and secondly its for a fixed number of lines (6), which may not always be the case:-
sed -i '/<name>gba<\/name>/{n;n;n;n;n;n;s/<\/extension>/ .sh<\/extension>/}' /tmp/test.txt