Utsav
Utsav

Reputation: 8103

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.

Upvotes: 5

Views: 2629

Answers (4)

bbarker
bbarker

Reputation: 13108

Still UNIX-related, though requiring the more modern and far from ubiquitous tool, Ammonite, you could use email-replace.

$ amm path/to/email-replace.sc <random integer seed> <file1 with emails> <file2 with emails> ...

DISCLAIMER: the matcher is likely far from perfect, so use at your own risk, and always have backups available. Note that by default it replaces emails with a new random e-mail address. To use a fixed email address, just replace the call to randEmail with a constant string.

Upvotes: 0

Mark Adelsberger
Mark Adelsberger

Reputation: 45719

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 [email protected],[email protected], 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.

Upvotes: 1

Fabrizio
Fabrizio

Reputation: 8043

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.

Upvotes: 2

slavik
slavik

Reputation: 1303

Try this

sed s/[^,@]*@[^,]*/[email protected]/g

and

echo '[email protected],[email protected],[email protected]' | sed s/[^,@]*@[^,]*/[email protected]/g

result

[email protected],[email protected],[email protected]

Upvotes: 0

Related Questions