Utsav Utsav - 9 months ago 40
Linux Question

How to replace all email addresses in a set of files with a generic email address

I have some scripts which have many email address in different domain (say

. I want to replace all of them with some generic email address in a common domain, say
, keeping rest of the script same.

I am using below in
but it doesn't seem to work. (it is returning same output as input, so looks like the search is not matching. However, when I tested the regex
in online tester, it seems to match email addresses.)


For example, I have 2 scripts

$ cat script1.sh
export SENDFROM="xyz@domain1.com" blah_4

$ cat script2.sh
echo foo|mailx -s "blah" pqr@domain2.com,def@domain.com,some@domain.com
foo abc@domain.com bar

My result after
sed -i
should be

$ cat script1.sh
export SENDFROM="genericid@domain.com" blah_4

$ cat script2.sh
echo foo|mailx -s "blah" genericid@domain.com,genericid@domain.com,genericid@domain.com
foo genericid@domain.com bar

I am using
Linux 3.10.0-327.28.2.el7.x86_64

Any suggestion please?

I managed to make it work with
. There were 2 problem with previous

  • The
    before it.

  • As file had other
    lines (for
    database connections), I had to append
    at the end, as all my
    addresses ended in

Answer Source

UPDATE - Other answers, and comments on this one, point out that you may have to take extra steps to enable shorthand character class matching; I'm used to doing regex in perl, where this just works, so didn't think to address that possibility. This answer only addresses how to improve the matching once you have the regex functioning.


While the problem of matching email addresses with regex can be very complex (and in fact in the most general case isn't possible with true regex), you probably can handle your specific case.

The problem with what you have is that \S matches any non-whitespace, so address@something.com,address@somethingelse.com, where two addresses have no whitespace between, matches incorrectly.

So there's a couple ways to go about it, based on knowing what sorts of addresses you realistically will see. One solution would be to replace both instances of \S with [^\s,] (note the lowercase s), which simply excludes , from the match as well as whitespace.