Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 domain1.com,domain2.com . I want to replace all of them with some generic email address in a common domain, say domain.com, keeping rest of the script same.

I am using below in sed 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 \S+@\S+/ in online tester, it seems to match email addresses.)

s/\S+@\S+/[email protected]/g

For example, I have 2 scripts

$ cat script1.sh
[email protected]
export SENDFROM="[email protected]" blah_4

$ cat script2.sh
echo foo|mailx -s "blah" [email protected],[email protected],[email protected]
 [email protected]
foo [email protected] bar

My result after sed -i should be

$ cat script1.sh
[email protected]
export SENDFROM="[email protected]" blah_4

$ cat script2.sh
echo foo|mailx -s "blah" [email protected],[email protected],[email protected]
 [email protected]
foo [email protected] bar

I am using Linux 3.10.0-327.28.2.el7.x86_64

Any suggestion please?

Update: I managed to make it work with 's/\S\+@\S\+.com/[email protected]/g'. There were 2 problem with previous search.

  • The + needed \ before it.
  • As file had other @ lines (for database connections), I had to append .com at the end, as all my addresses ended in .com
like image 932
Utsav Avatar asked Oct 17 '22 22:10

Utsav


1 Answers

Capturing email adresses using regex can be more difficult than it seems. Anyhow, for replacing the domain, I think you could simplistically consider that an email domain starts when it find:

1 alphanum char + @ + N alphanum chars + . + N alphanum chars

Based on this preconception, in javascript I would do so:

(\w@)(\w*.\w*)

Replacing with:

$1newdomain.com

Hope it helps you.

like image 50
Fabrizio Avatar answered Nov 01 '22 12:11

Fabrizio