1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > linux sed 单引号 如何在SED中转义双引号和单引号? (bash)

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

时间:2019-10-07 05:25:10

相关推荐

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

转义双引号ca在sed中绝对有必要:例如,如果在整个sed表达式中使用双引号(因为在使用shell变量时需要执行此操作)。

下面是对SED逃逸倒是一个例子,但也捕获在bash一些其他的报价问题:

# cat inventory

PURCHASED="-09-01"

SITE="Atlanta"

LOCATION="Room 154"

比方说,你想用sed脚本,您可以用遍地改变房间,所以你variablize输入如下:

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

这个脚本会增加整个行,如果它不存在,或者它会简单地替换(使用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="your location"; if grep -q LOCATION inventory; then sed -i "/^LOCATION/c\LOCATION=\"$i\"" inventory; else echo LOCATION='"'$i'"' >> inventory; fi

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。