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

您所在的位置:网站首页 linux单引号转义 linux sed 单引号,如何在SED中转义双引号和单引号? (bash)

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

2023-07-19 19:42| 来源: 网络整理| 查看: 265

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

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

# cat inventory

PURCHASED="2014-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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3