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

sed replace all
sed replace line
sed replace character

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+/genericid@domain.com/g

For example, I have 2 scripts

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

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

My result after sed -i should be

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

$ cat script2.sh
echo foo|mailx -s "blah" genericid@domain.com,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?

Update: I managed to make it work with 's/\S\+@\S\+.com/genericid@domain.com/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

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.

Postfix Address Rewriting, For example, replace the address of an alias by the addresses listed under that alias. Generic mapping for outgoing SMTP mail. Address rewriting with local delivery. Local alias database; Local per-user .forward files; Local catch-all address. Remove an email address from a user mailbox. Use the EAC to remove an email address. In the EAC, navigate to Recipients > Mailboxes. In the list of user mailboxes, click the mailbox that you want to remove an email address from, and then click Edit . On the mailbox properties page, click Email Address.


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.

How to Find or Validate an Email Address, All the email addresses it matches can be handled by 99% of all email software out there. it suitable for extracting email addresses from files or larger blocks of text. If you want to check whether the user typed in a valid email address, replace The non-capturing group makes the middle of the domain and the final letter or​  Then just add users to the group that you want to recieve emails to that group. Generally people have department groups in their AD and just mail enable them. Though at times you may not want every sales person to get emails to Sales@aaa.com, maybe you just want 2 people or just a manager or so on.


Try this

sed s/[^,@]*@[^,]*/genericid@domain.com/g

and

echo 'pqr@domain2.com,def@domain.com,some@domain.com' | sed s/[^,@]*@[^,]*/genericid@domain.com/g

result

genericid@domain.com,genericid@domain.com,genericid@domain.com

Sending emails to a large group of bcc'd email addresses?, Gmail is NOT a bulk e-mail provider so repeatedly sending bulk messages to an error “69585” or as having “reached a limit for sending mail”. can be extra restrictive for new accounts, or accounts with a change in typical usage. out the message or a generic email address available to all ride captains. Tech support scams are an industry-wide issue where scammers trick you into paying for unnecessary technical support services. You can help protect yourself from scammers by verifying that the contact is a Microsoft Agent or Microsoft Employee and that the phone number is an official Microsoft global customer service number.


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.

How to Send Out Email Without Showing Addresses, All email clients give you the ability to hide recipients by entering a list of email addresses in the "Bcc" or "Blind Carbon Copy" field. The procedure for adding  Learn how to scan and send jobs to one, or multiple, email addresses from the printer control panel of HP Enterprise MFPs. Learn more about scanning and sending documents to email on HP Enterprise


Whitelist Email Addresses in an Email Client or Security Program, Select "Customize Mail Controls for this Screen Name" and click Next. (Optional) Select "Allow email from all AOL members, email addresses  4. Locate the person on the addresses list you wish to remove, tap it to highlight the person and then tap the “Delete” button: 5. Tap “Yes” to confirm the deletion. The position where this person was will now be free to add someone else.


The Networking CD Bookshelf, To support SMTP mail, you must have the MAILER(smtp) macro in your nine other sets of mailers available with the MAILER command, all of which are covered in Edit the work file to change the OSTYPE line from unknown to the correct value for With the exception of how UUCP addresses are handled, the output file  How to Open an EMAIL File. EMAIL files can be opened by Windows Live Mail, part of the old, free Windows Essentials suite. An older version of this program, Microsoft Outlook Express, will also open EMAIL files. This Windows Essential suite has been discontinued by Microsoft but can still be found in some places.


The Best Way to Switch to a New Email Address, Whatever the situation may be, switching to a new email address is Email all contacts with the new email address; Update all online accounts that can import your entire PST file into Gmail by adding your Gmail account to  Domain Emails. Get Useful Information In Seconds. Visit Today & Quickly Get More Results On Fastquicksearch.com