I'm using the Git Bash shell on Windows, and trying to replace a string like this in an XML file using sed:
<customTag>C:\path\to\2016a.0</customTag> To a string like this:
<customTag>C:\path\to\2017b.0</customTag> I can do the replacement directly like this:
$ cat test.txt <customTag>C:\path\to\2016a.0</customTag> $ sed -i 's^<customTag>C:\\path\\to\\2016a.0</customTag>^<customTag>C:\\path\\to\\2017b.0</customTag>^g' test.txt $ cat test.txt <customTag>C:\path\to\2017b.0</customTag> But if I need to pass in variables for those strings, the replacement doesn't work.
$ cat test.txt <customTag>C:\path\to\2016a.0</customTag> $ export OLD_VER=2016a.0 $ export NEW_VER=2017b.0 $ sed -i 's^<customTag>C:\\path\\to\\${OLD_VER}</customTag>^<customTag>C:\\path\\to\\${NEW_VER}</customTag>^g' test.txt $ cat test.txt <customTag>C:\path\to\2016a.0</customTag> Or if I use double quotes around the sed expression, I get "Invalid back reference", presumably because it thinks the 2 in the year is a reference.
$ sed -i "s^<customTag>C:\\path\\to\\${OLD_VER}</customTag>^<customTag>C:\\path\\to\\${NEW_VER}</customTag>^g" test.txt sed: -e expression #1, char 87: Invalid back reference What's the correct way to escape or quote this, or would I be better off using something like awk?
's/xxx'"$var1"'/yyy'"$var2"'/'