Spartan-196
Spartan-196

Reputation: 353

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. I am trying to get sed to match the default webmaster email addresses in the .conf file which works great with this 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. I test this by running the same egrep command again.

egrep -o '\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+(\.[A-Za-z]{2,4})?\b' /home/test/httpd.conf

returns

[email protected]
root@localhost
[email protected]

The sed command I'm trying to use is

sed -i '/ServerAdmin/ s/\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+(\.[A-Za-z]{2,4})?\b/[email protected]/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.

Upvotes: 1

Views: 251

Answers (2)

Mark Reed
Mark Reed

Reputation: 95242

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. It's easy to use sed as if it were a grep command: sed -ne '/pattern/p'.

Upvotes: 1

Spartan-196
Spartan-196

Reputation: 353

sed must be told that it needs to use extended regular expressions using the -r option then making the sed command as follows.

sed -ir '/ServerAdmin/ s/\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+(\.[A-Za-z]{2,4})?\b/[email protected]/g' /home/test/httpd.conf

Much thanks to Kent for pointing out that the address it was missing wasnt following a ServerName

Upvotes: 0

Related Questions