1

I need to modify an xml file using Sed to replace the line

url="jdbc:oracle:thin:@//ttpdbscan.axel.net:1521/axel.telco.net"

with

url="jdbc:oracle:thin:@//ttpdbscan.axeltelecom.net:1598/axelPRD.telco.net"

I have stored the lines like this

ACTUAL_DB=$(sed -n 's#^.*url="\(.*\).*"#\1#p' $FILE.xml)

and

NEW_DB="jdbc:oracle:thin:@//ttpdbscan.axeltelecom.net:1598/axelPRD.telco.net"

And the replacing method is this one

sed -i "s#$ACTUAL_DB#$NEW_DB#g" $File.xml

The problem is that when I run the script the file stays the same.

I have echoed the variables and all of them return the correct values.

Inian
  • 80,270
  • 14
  • 142
  • 161
  • although using `sed` to modify xml isn't advisable (use tools like xmlstarlet instead), I don't see any issue for the given sample and sed commands used.. – Sundeep Jun 10 '20 at 05:05
  • what happens if you do `sed "s#$ACTUAL_DB#$NEW_DB#g" $File.xml > new.xml` does the new file has modified content? – Sundeep Jun 10 '20 at 05:06
  • Can you please add output of `echo ${ACTUAL_DB}` – Digvijay S Jun 10 '20 at 05:34
  • `sed -i "s#${ACTUAL_DB}#${NEW_DB}#g" File.xml`? If `File.xml` is a path, you should not add `$` there. – Wiktor Stribiżew Jun 10 '20 at 08:51
  • when I echoed ACTUAL_DB, it shows the correct value. I made a test where I created another variable and assigned the hardcoded text the sed works with that new variable, and if I compare it to the one I'm getting it shows that they are the same – Axel Galindo Jun 10 '20 at 14:07

2 Answers2

0

Assuming the file you have is File.xml (if it is not a variable), you may use

sed -i "s#${ACTUAL_DB}#${NEW_DB}#g" File.xml

Try also with other delimiters:

sed -i "s~${ACTUAL_DB}~${NEW_DB}~g" File.xml

If your sed does not support -i use

sed "s~${ACTUAL_DB}~${NEW_DB}~g" File.xml 1<> File.xml

See sed edit file in place

Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37
0

So I saved the output into another file and found out that the string had an extra space

so it looked like this

ACTUAL_DB= "jdbc:oracle:thin:@//ttpdbscan.axel.net:1521/axel.telco.net "

I removed the extra space with

"$(echo -e "${ACTUAL_DB}" | tr -d '[:space:]')"

And now the sed is working as intended