bash 如何在 sed 中转义双引号和单引号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7517632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:03:46  来源:igfitidea点击:

How do I escape double and single quotes in sed?

bashcommand-linesed

提问by KRB

From what I can find, when you use single quotes everything inside is considered literal. I want that for my substitution. But I also want to find a string that has single or double quotes.

据我所知,当您使用单引号时,里面的所有内容都被视为文字。我想要那个作为我的替代品。但我也想找到一个带有单引号或双引号的字符串。

For example,

例如,

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

I want to replace "http://www.fubar.com" with URL_FUBAR. How is sed supposed to recognize my // or my double quotes?

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

Thanks for any help!

谢谢你的帮助!

EDIT:Could I use s/\"http\:\/\/www\.fubar\.\com\"/URL_FUBAR/g?

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

Does \ actually escape chars inside the single quotes?

\ 是否真的在单引号内转义字符?

回答by Kusalananda

The s///command in sedallows you to use other characters instead of /as the delimiter, as in

s///命令sed允许您使用其它字符,而不是/作为分隔符,如

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 sedis to interpret them as literal dots and not as the regular expression pattern .which matches any one character.

如果sed要将这些点解释为文字点而不是.匹配任何一个字符的正则表达式模式,则需要对这些点进行转义。

回答by tony duan

Regarding the single quote, see the code below used to replace the string let'swith let us:

关于单引号,请参阅下面用来替换字符串代码let'slet us

command:

命令:

echo "hello, let's go"|sed 's/let'"'"'s/let us/g'

result:

结果:

hello, let us go

你好,让我们走

回答by glenn Hymanman

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

很难在单引号中转义单引号。尝试这个:

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

Example:

例子:

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

produces

产生

this is URL_FUBAR and URL_EXAMPLE here

回答by Denis

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 Hymanman that you replace the "inside the sed regex with [\"].

我的问题是我需要""在表达式之外,因为我在 sed 表达式本身内部有一个动态变量。因此,实际的解决方案是 lenn Hymanman 的一个,您"将 sed 正则表达式的内部替换为[\"].

So my complete bash is:

所以我完整的 bash 是:

RELEASE_VERSION="0.6.6"

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

Here is:

这是:

#is the sed separator

#是 sed 分隔符

[\"]= "in regex

[\"]= "在正则表达式中

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

value ==\"tags/$RELEASE_VERSION\"我的替换字符串,重要的是它只有\"引号

回答by starry_eyed_sysadmin

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).

在 sed 中转义双引号绝对是必要的:例如,如果您在整个 sed 表达式中使用双引号(当您想使用 shell 变量时需要这样做)。

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

这是一个示例,它涉及在 sed 中转义,但也捕获了 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:

假设您想使用可以反复使用的 sed 脚本更改房间,因此您可以按如下方式对输入进行变量化:

# 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.

如果整行不存在,该脚本将添加整行,或者它会简单地用文本加上 $i 的值替换(使用 sed)存在的行。

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:

PS 我在多行上写出上面的脚本以使注释可解析,但我将其用作命令行上的单行,如下所示:

i="your location"; if grep -q LOCATION inventory; then sed -i "/^LOCATION/c\LOCATION=\"$i\"" inventory; else echo LOCATION='"'$i'"' >> inventory; fi

回答by SachinH

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%

回答by Victoria Stuart

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

"旁白:包含 BASH 变量的 sed 表达式需要用双 ( ) 引用才能正确解释变量。

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

如果您双引号 $BASH 变量(推荐做法)

... 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.)

(简单地 -escaping"$VAR"作为\"$VAR\"结果在"-quoted 输出字符串。)



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):

在这里, $VAR"在管道到 sed 之前被引用(sed 是'- 或"-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)
  • "-引用 BASH 变量 ( "$VAR")
  • "-quote,再次,那些变量 ( ""$VAR""),如果它们在 sed 表达式中使用(它本身必须被"-quoted,而不是'-quoted)
$ VAR='apples and bananas'

$ echo "$VAR"
apples and bananas

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

回答by Guilhem Hoffmann

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

您需要使用 \" 来转义 " 字符(\ 转义以下字符

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

回答by Giuseppe La Rosa

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

可能是“\”字符,试试这个:

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