2

I have an xml file with line below

<config-root>/temp</config-root>

Using 'sed' is bash shell script I want to replace the line, the 'sed' script is below

sed -i 's/<config-root>\(.*\)<\/config-root>/<config-root>\"${dirPath}"<\/config-root>/' Plan.xml

The 'sed' is resulting in

<config-root>"${dirPath}"</config-root>

I am expecting the line to be replaced as /opt/shared

Can anyone let me know what is wrong in my script? Basically I want to use variable in ‘sed’

Thanks in advance, Babu

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
Tech Tech
  • 141
  • 2
  • 10

1 Answers1

1

You can use bash to place the variable in the sed script: End the sed script using the single quote ', place the variable in double quotes " and continue the sed program with another single quote ':

sed 's~<config-root>[^<]*</config-root>~<config-root>'"$variable"'</config-root>~' Plan.xml

I would encourage you to use delimiter different from / because the / is part of the pattern (and of the variable) and would need to get escaped otherwise. I used ~ as the delimiter.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266