How to use verbatim strings in sed?
You should escape the special characters and use a separator other than "/".
sed 's#string1#string2#'
See this SOq:
- https://stackoverflow.com/questions/407523/escape-a-string-for-sed-search-pattern
This answer on the above:
- https://stackoverflow.com/a/407649/438544
is something you need, except you'd do it in the first, not second part of s/first/second/
.
As you are using both '
and "
in your sed commands, you'll have to escape some of it. Try doing this - make two files:
1.sed
s_"a\\c:ti]\\']x""/\\//:`~\$%#\^&"'_ _g
2.txt
"a\c:ti]\']x""/\//:`~$%#^&"'hello world m"a\c:ti]\']x""/\//:`~$%#^&"'
"a\c:ti]\']x""/\//:`~$%#^&"'this is working"a\c:ti]\']x""/\//:`~$%#^&"'
"a\c:ti]\']x""/\//:`~$%#^&"'as expected"a\c:ti]\']x""/\//:`~$%#^&"'
1.sed is the script itself and 2.txt is a test file. Run it like this to test:
$ sed -f 1.sed 2.txt
hello world m
this is working
as expected
$
Hope this helps.