Invalid preceding regular expression given by sed
bash, command-line, regex, sed
Solution
It appears the GNU `sed` does not like the PCRE non-capturing notation:
...(?:...)...
Try:
s/\b[A-Za-z0-9._%-]+@([a-zA-Z0-9-]+\.)+(\.[A-Za-z]]{2,4})?\b/email@example.com/
GNU `sed` seems to be OK with that. However, you still have a little work to do. Given the first line below as input, the output is the second line:
abc def@ghi.jk aaa
abc email@example.comjk aaa
There are two problems giving that result:
- The `]]` should be a single `]`.
- You're looking for a trailing dot in the prior regex, so you don't want one in the last part of the domain suffix.
This does the job:
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
Problem
I am and have been working on a sed script file and I am running into an "Invalid Preceding regular expression" error when I run it. Below is the file in its entirety. I have done much search on this already, both on this site and else where. Many questions asked here have resulted in needing to be either extend regular expressions something being escaped incorrectly. I have defined this as a extended expresion already as it is needed for the email substitution. ``` #!/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/ ``` I am calling it from termal as ./config-apache.sed /etc/httpd/conf/httpd.conf and get this returned. ``` /bin/sed: file ./apache-install.sed line 12: Invalid preceding regular expression ``` inside of vim line 12 is i dentified as the single `}` above `#Enable user's Public HTML directories`