[英]SED command matches regex but does not substitute
I am working on building a .sed file to start scripting the setup of multiple apache servers. 我正在构建.sed文件,以开始编写多个apache服务器设置的脚本。 I am trying to get sed to match the default webmaster email addresses in the .conf file which works great with this egrep.
我正在尝试使sed与.conf文件中的默认网站管理员电子邮件地址匹配,该文件非常适合此egrep。 However when I use sed to try and so a substitute search and replace i get no errors back but it also does not do any substituting.
但是,当我使用sed尝试进行替代搜索和替换时,我没有收到任何错误,但也没有任何替代。 I test this by running the same egrep command again.
我通过再次运行相同的egrep命令进行测试。
egrep -o '\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+(\.[A-Za-z]{2,4})?\b' /home/test/httpd.conf
returns 回报
admin@your-domain.com
root@localhost
webmaster@dummy-host.example.com
The sed command I'm trying to use is 我要使用的sed命令是
sed -i '/ServerAdmin/ s/\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+(\.[A-Za-z]{2,4})?\b/MY_ADMIN_ADDRESS@gmail.com/g' /home/test/httpd.conf
After running I try and verify the results by running the egrep again and it returns the same 3 email address indicating nothing was replaced. 运行后,我尝试通过再次运行egrep来验证结果,它返回相同的3电子邮件地址,表示没有替换任何内容。
Don't assume that any two tools use the same regular expression syntax. 不要假设任何两个工具使用相同的正则表达式语法。 If you're going to be doing replacements with
sed
, use sed
to test - not egrep
. 如果您打算用
sed
进行替换,请使用sed
进行测试,而不是egrep
。 It's easy to use sed
as if it were a grep
command: sed -ne '/pattern/p'
. sed
就像grep
命令一样容易使用: sed -ne '/pattern/p'
。
sed must be told that it needs to use extended regular expressions using the -r option then making the sed command as follows. 必须告知sed它需要使用-r选项使用扩展的正则表达式,然后按以下方式执行sed命令。
sed -ir '/ServerAdmin/ s/\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+(\.[A-Za-z]{2,4})?\b/MY_ADMIN_ADDRESS@gmail.com/g' /home/test/httpd.conf
Much thanks to Kent for pointing out that the address it was missing wasnt following a ServerName
非常感谢Kent指出,缺少的地址不是
ServerName
之后的地址
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.