sed 单引号
我一直在尝试使用 SED(Bash shell)来逃避单引号的问题。
我需要将我尝试过的内容变成
$cfg['Servers'][$i]['password'] = '';
:
$cfg['Servers'][$i]['password'] = 'mypassword';
这
sed -i "s/$cfg['Servers'][$i]['password'] = '';/$cfg['Servers'][$i]['password'] = '$rootpassword';/g" /usr/share/phpmyadmin/libraries/config.default.bak
最终会让线路变得混乱。
$cfg['Servers'][$i]['password['Servers'][]['passsword'] = 'mypassword'
我已经尝试过使用 '\'' 来转义单引号,我认为阳光下的其他一切都可以,但就是无法完全实现。
有人能指出我可能存在的明显错误吗?
谢谢。
I've been banging into escaping single quote's problem using SED (Bash shell).
I need to make
$cfg['Servers'][$i]['password'] = '';
into
$cfg['Servers'][$i]['password'] = 'mypassword';
What I've tried is:
sed -i "s/$cfg['Servers'][$i]['password'] = '';/$cfg['Servers'][$i]['password'] = '$rootpassword';/g" /usr/share/phpmyadmin/libraries/config.default.bak
Which ends up really jumbling the line.
$cfg['Servers'][$i]['password['Servers'][]['passsword'] = 'mypassword'
I've tried the '\'' to escape single quotes and I think everything else under the sun but just can't get it quite there.
can anyone point to my probable obvious mistake?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
\x27
作为单引号,而不是转义。这不仅适用于 sed、awk 等。请参阅测试:
请注意,sed 行可能不是您问题的最佳解决方案,例如,可能不需要将整个字符串放入“s/../”。然而,我只是想展示 \x27 是如何工作的。
instead of escaping, you can use
\x27
for single quote. this works not only for sed, for awk etc. as well.see test:
note that, the sed line may not be the best solution for your problem, e.g. put the whole string to "s/../" may not be necessary. However, I just want to show how the \x27 worked.
试试这个:
匹配包含“anything..Servers..anything..password..anything..''”的行(以
''
结尾),并在该行替换''
和'foo'
这可以匹配多行,但只有第一个出现的会被更改。
测试一下,很可能
.. Servers .. password .. ''
只在一行上。或者你可以逃避一切。
Try this:
Match a line that contains "anything..Servers..anything..password..anything..''" (end with
''
) and on that line replace''
with'foo'
This can match more than one lines, but only the first occurance will be changed.
Test it, it's most probable that
.. Servers .. password .. ''
is only on one line.Or you can just escape everything.