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

1
  • Use -r or -E switch to enable ERE. Use different delimiters to avoid crazy escaping. Commented Mar 27, 2014 at 20:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you , with your suggestion it is working

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.