sed 单引号

发布于 2024-12-21 06:27:01 字数 610 浏览 1 评论 0原文

我一直在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感受沵的脚步 2024-12-28 06:27:01

您可以使用 \x27 作为单引号,而不是转义。这不仅适用于 sed、awk 等。

请参阅测试:

kent$  cat tt
$cfg['Servers'][$i]['password'] = ''

kent$  sed -r 's/(\$cfg\[\x27Servers\x27\]\[\$i\]\[\x27password\x27\] \= \x27)\x27/\1mypassword\x27/g' tt
$cfg['Servers'][$i]['password'] = 'mypassword'

请注意,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:

kent$  cat tt
$cfg['Servers'][$i]['password'] = ''

kent$  sed -r 's/(\$cfg\[\x27Servers\x27\]\[\$i\]\[\x27password\x27\] \= \x27)\x27/\1mypassword\x27/g' tt
$cfg['Servers'][$i]['password'] = 'mypassword'

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.

蓝礼 2024-12-28 06:27:01
$ sed -i "s/\$cfg\['Servers'\]\[\$i\]\['password'\] = '';/\$cfg['Servers'][\$i]['password'] = '\$rootpassword';/g" file
$ sed -i "s/\$cfg\['Servers'\]\[\$i\]\['password'\] = '';/\$cfg['Servers'][\$i]['password'] = '\$rootpassword';/g" file
奢望 2024-12-28 06:27:01

试试这个:

sed -i "/Servers.*password.*''$/s/''/'foo'/" /path/to/your/testfile

匹配包含“anything..Servers..anything..password..anything..''”的行(以 '' 结尾),并在该行替换 '''foo'

这可以匹配多行,但只有第一个出现的会被更改。
测试一下,很可能 .. Servers .. password .. '' 只在一行上。

或者你可以逃避一切。

Try this:

sed -i "/Servers.*password.*''$/s/''/'foo'/" /path/to/your/testfile

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文