如何在 sed 中转义斜杠、双引号和单引号?

据我所知,当你使用单引号时,里面所有的东西都被认为是字面的。我想用这个代替我的位置。但是我还想找到一个包含单引号或双引号的字符串。

比如说,

sed -i 's/"http://www.fubar.com"/URL_FUBAR/g'

我想用 URL _ FUBAR 替换“ http://www.fubar.com”。 sed 应该如何识别我的//或双引号?

谢谢你的帮助!

编辑: 我可以使用 s/\"http\:\/\/www\.fubar\.\com\"/URL_FUBAR/g吗?

在单引号中实际转义字符吗?

196846 次浏览

May be the "\" char, try this one:

sed 's/\"http:\/\/www.fubar.com\"/URL_FUBAR/g'

You need to use \" for escaping " character (\ escape the following character

sed -i 's/\"http://www.fubar.com\"/URL_FUBAR/g'

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.

It's hard to escape a single quote within single quotes. Try this:

sed "s@['\"]http://www.\([^.]\+).com['\"]@URL_\U\1@g"

Example:

$ sed "s@['\"]http://www.\([^.]\+\).com['\"]@URL_\U\1@g" <<END
this is "http://www.fubar.com" and 'http://www.example.com' here
END

produces

this is URL_FUBAR and URL_EXAMPLE here

My problem was that I needed to have the "" outside the expression since I have a dynamic variable inside the sed expression itself. So than the actual solution is that one from lenn jackman that you replace the " inside the sed regex with [\"].

So my complete bash is:

RELEASE_VERSION="0.6.6"


sed -i -e "s#value=[\"]trunk[\"]#value=\"tags/$RELEASE_VERSION\"#g" myfile.xml

Here is:

# is the sed separator

[\"] = " in regex

value = \"tags/$RELEASE_VERSION\" = my replacement string, important it has just the \" for the quotes

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

Escaping a double quote can absolutely be necessary in sed: for instance, if you are using double quotes in the entire sed expression (as you need to do when you want to use a shell variable).

Here's an example that touches on escaping in sed but also captures some other quoting issues in bash:

# cat inventory
PURCHASED="2014-09-01"
SITE="Atlanta"
LOCATION="Room 154"

Let's say you wanted to change the room using a sed script that you can use over and over, so you variablize the input as follows:

# i="Room 101" (these quotes are there so the variable can contains spaces)

This script will add the whole line if it isn't there, or it will simply replace (using sed) the line that is there with the text plus the value of $i.

if grep -q LOCATION inventory; then
## The sed expression is double quoted to allow for variable expansion;
## the literal quotes are both escaped with \
sed -i "/^LOCATION/c\LOCATION=\"$i\"" inventory
## Note the three layers of quotes to get echo to expand the variable
## AND insert the literal quotes
else
echo LOCATION='"'$i'"' >> inventory
fi

P.S. I wrote out the script above on multiple lines to make the comments parsable but I use it as a one-liner on the command line that looks like this:

i="your location"; if grep -q LOCATION inventory; then sed -i "/^LOCATION/c\LOCATION=\"$i\"" inventory; else echo LOCATION='"'$i'"' >> inventory; fi
Prompt% cat t1
This is "Unix"
This is "Unix sed"
Prompt% sed -i 's/\"Unix\"/\"Linux\"/g' t1
Prompt% sed -i 's/\"Unix sed\"/\"Linux SED\"/g' t1
Prompt% cat t1
This is "Linux"
This is "Linux SED"
Prompt%

Aside: sed expressions containing BASH variables need to be double (")-quoted for the variable to be interpreted correctly.

If you also double-quote your $BASH variable (recommended practice)

... then you can escape the variable double quotes as shown:

sed -i "s/foo/bar ""$VARIABLE""/g" <file>

I.e., replace the $VARIABLE-associated " with "".

(Simply -escaping "$VAR" as \"$VAR\" results in a "-quoted output string.)


Examples

$ VAR='apples and bananas'
$ echo $VAR
apples and bananas


$ echo "$VAR"
apples and bananas


$ printf 'I like %s!\n' $VAR
I like apples!
I like and!
I like bananas!


$ printf 'I like %s!\n' "$VAR"
I like apples and bananas!

Here, $VAR is "-quoted before piping to sed (sed is either '- or "-quoted):

$ printf 'I like %s!\n' "$VAR" | sed 's/$VAR/cherries/g'
I like apples and bananas!


$ printf 'I like %s!\n' "$VAR" | sed 's/"$VAR"/cherries/g'
I like apples and bananas!


$ printf 'I like %s!\n' "$VAR" | sed 's/$VAR/cherries/g'
I like apples and bananas!


$ printf 'I like %s!\n' "$VAR" | sed 's/""$VAR""/cherries/g'
I like apples and bananas!


$ printf 'I like %s!\n' "$VAR" | sed "s/$VAR/cherries/g"
I like cherries!


$ printf 'I like %s!\n' "$VAR" | sed "s/""$VAR""/cherries/g"
I like cherries!

Compare that to:

$ printf 'I like %s!\n' $VAR | sed "s/$VAR/cherries/g"
I like apples!
I like and!
I like bananas!




$ printf 'I like %s!\n' $VAR | sed "s/""$VAR""/cherries/g"
I like apples!
I like and!
I like bananas!

... and so on ...

Conclusion

My recommendation, as standard practice, is to

  • "-quote BASH variables ("$VAR")
  • "-quote, again, those variables (""$VAR"") if they are used in a sed expression (which itself must be "-quoted, not '-quoted)
$ VAR='apples and bananas'


$ echo "$VAR"
apples and bananas


$ printf 'I like %s!\n' "$VAR" | sed "s/""$VAR""/cherries/g"
I like cherries!


You can use %

sed -i "s%http://www.fubar.com%URL_FUBAR%g"

What finally worked for me was to use sed's HEX option to represent:

Single quote:

echo "book'" |sed -n '/book[\x27]/p'

book'

Double quotes solution

cat testfile

"http://www.fubar.com"

sed -i 's|"http://www\.fubar\.com"|URL_FUBAR|g' testfile

cat testfile

URL_FUBAR