17

我正在并且一直在处理 sed 脚本文件,当我运行它时遇到“无效的前面的正则表达式”错误。以下是整个文件。

我已经对此进行了很多搜索,无论是在这个网站上还是在其他地方。这里提出的许多问题导致需要扩展正则表达式,某些东西被错误地转义。我已经将其定义为扩展表达式,因为它是电子邮件替换所需要的。

#!/bin/sed -rf
#/find_this thing/{
#s/ search_for_this/ replace_with_this/
#s/ search_for_this_other_thing/ replace_with_this_other_thing/
#}

#Search and replace #ServerAdmin (with preceding no space) email addresses using a regular expression that has the .com .net and so on domain endings as option so it will find root@localhost and replace it in line with admin's email address.

ServerAdmin/ { 
s/\b[A-Za-z0-9._%-]+@(?:[a-zA-Z0-9-]+\.)+(\.[A-Za-z]]{2,4})?\b/email@example.com/
}
#Enable user's Public HTML directories
/UserDir/ {
s/disable$/enable/ 
s/^#User/User/
}
#Replace the only #ServerName (with preceding no space) followed space and text with Our server ip
/#ServerName */ c\ ServerName server.ip.address.here/

我从 termal 中将其称为 ./config-apache.sed /etc/httpd/conf/httpd.conf 并返回它。

/bin/sed: file ./apache-install.sed line 12: Invalid preceding regular expression

}vim 第 12 行的内部被标识为上面的单行#Enable user's Public HTML directories

4

1 回答 1

24

看来 GNUsed不喜欢 PCRE 非捕获符号:

...(?:...)...

尝试:

s/\b[A-Za-z0-9._%-]+@([a-zA-Z0-9-]+\.)+(\.[A-Za-z]]{2,4})?\b/email@example.com/

GNUsed似乎对此没意见。但是,您还有一些工作要做。给定下面的第一行作为输入,输出是第二行:

abc def@ghi.jk aaa
abc email@example.comjk aaa

给出这个结果有两个问题:

  1. 应该]]是一个.]
  2. 您正在前面的正则表达式中寻找一个尾随点,因此您不希望在域后缀的最后部分出现一个点。

这可以完成工作:

s/\b[A-Za-z0-9._%-]+@([a-zA-Z0-9-]+\.)+([A-Za-z]{2,4})?\b/email@example.com/

abc def@ghi.jk aaa
abc email@example.com aaa
于 2013-02-18T04:58:50.617 回答