How do I escape double and single quotes in sed?
Solution 1:
The s///
command in sed
allows you to use other characters instead of /
as the delimiter, as in
sed 's#"http://www\.fubar\.com"#URL_FUBAR#g'
or
sed 's,"http://www\.fubar\.com",URL_FUBAR,g'
The double quotes are not a problem. For matching single quotes, switch the two types of quotes around. Note that a single quoted string may not contain single quotes (not even escaped ones).
The dots need to be escaped if sed
is to interpret them as literal dots and not as the regular expression pattern .
which matches any one character.
Solution 2:
Regarding the single quote, see the code below used to replace the string let's
with let us
:
command:
echo "hello, let's go"|sed 's/let'"'"'s/let us/g'
result:
hello, let us go