Issue
I want to replace first occurence of xml tag value with different value. Values which comes from the environment variable from shell.
My environment values looks like TEMP1=ghi://1234 TEMP2=5678. So first value occurence has to replace with TEMP1 and TEMP2 by using sed/awk.
For example
<a>
<value>abcd:efgh</value>
</a>
<b>
<value>mnop:qrst</value>
</b>
Output :
<a>
<value>ghi://1234:5678</value>
</a>
<b>
<value>mnop:qrst</value>
</b>
Solution : It is not allowing me to replace environment variable.
sed -i '0,/<value>.*<\/value>/s//<value>$TEMP1:TEMP2<\/value>/' inputfile.txt
Solution
If you want to expand variable, place the statement in double quotes. Also, as there are forward slashes in the text, change the separator to something else i.e. @ and so:
sed -i "0,/<value>.*<\/value>/s@@<value>$TEMP1:$TEMP2<\/value>@" inputfile.txt
Finally, you should really amend XML with a proper XML parser such as xmllint or xmlstarlet
Answered By - Raman Sailopal