如何在SED中转义双引号和单引号?(bash)

时间:2022-09-15 15:07:54

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?

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

8 个解决方案

#1


63  

The sed command allows you to use other characters instead of /:

sed命令允许您使用其他字符,而不是/:

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

The double quotes are not a problem.

双引号不是问题。

#2


15  

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

关于单引号,请参见下面用来替换字符串let's的代码:

command:

命令:

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

result:

结果:

hello, let us go

你好,我们走吧

#3


10  

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

#4


7  

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 [\"].

我的问题是,我需要在表达式外面加上“”,因为在sed表达式本身中有一个动态变量。所以实际的解决方案是用[\]替换lenn jackman的“sed regex内部”。

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

#是sed分隔符

[\"] = " in regex

[\"] = "在regex中

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

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

#5


1  

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:

附注:我把上面的脚本写在多行上以使注释可以被解析,但是我把它作为命令行上的一行代码,看起来是这样的:

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

#6


0  

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%

#7


-1  

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

您需要使用\“for escape”字符(\转义以下字符)。

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

#8


-2  

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

可能是"\" char,试试这个:

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

#1


63  

The sed command allows you to use other characters instead of /:

sed命令允许您使用其他字符,而不是/:

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

The double quotes are not a problem.

双引号不是问题。

#2


15  

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

关于单引号,请参见下面用来替换字符串let's的代码:

command:

命令:

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

result:

结果:

hello, let us go

你好,我们走吧

#3


10  

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

#4


7  

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 [\"].

我的问题是,我需要在表达式外面加上“”,因为在sed表达式本身中有一个动态变量。所以实际的解决方案是用[\]替换lenn jackman的“sed regex内部”。

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

#是sed分隔符

[\"] = " in regex

[\"] = "在regex中

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

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

#5


1  

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:

附注:我把上面的脚本写在多行上以使注释可以被解析,但是我把它作为命令行上的一行代码,看起来是这样的:

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

#6


0  

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%

#7


-1  

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

您需要使用\“for escape”字符(\转义以下字符)。

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

#8


-2  

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

可能是"\" char,试试这个:

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