bash sed 查找并用空格替换字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24754610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:52:19  来源:igfitidea点击:

sed find and replace a string with spaces

regexlinuxbashreplacesed

提问by Alex

Having the following in a file:

在文件中包含以下内容:

public $password = 'XYZ';

I'm trying to replace the password's value with a different one, through an automated deployment process from backup files.

我正在尝试通过备份文件的自动部署过程将密码的值替换为不同的值。

I have the regext that will match the string above in a file, but not much compatible with sed

我有一个与文件中的上述字符串相匹配的正则表达式,但与 sed

(public\s$password\s=\s'(.*)'?)

I also tried

我也试过

sed -i -e "s/public\s$password\s=\s'(.*)'/private\s$password\s=\s'jingle'" configuration.php

Any ideas?

有任何想法吗?

回答by ams

Try this:

尝试这个:

sed -i -e "s/public\s$password\s=\s'\(.*\)'/private $password = 'jingle'/" configuration.php

The problem was that you need to 'escape' the round brackets, and that \sdoesn't work in the output pattern. You also had missed the final /.

问题是您需要“转义”圆括号,这\s在输出模式中不起作用。你也错过了决赛/