Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get sed to anonymize all e-mail addresses in file

Tags:

regex

sed

I'm looking for a way to replace all e-mail addresses in a file with [email protected] I guess sed is the right tool for the job, but I'm unable to find the way to do it in a robust and correct way. Thanks.

like image 898
grm Avatar asked Aug 21 '13 10:08

grm


3 Answers

Here is one crude way of doing that using sed -r:

sed -r 's/^(.*? |)[^@]+@[^ ]+/\[email protected]/g' file

On BSD (eg OSX) use this variant:

sed -E 's/(^|.* )[^@]+@[^ ]+/\[email protected]/g' file

Though keep in mind email addresses can vary a lot these days.

like image 50
anubhava Avatar answered Nov 15 '22 02:11

anubhava


The following worked for me:

sed "s/[^@ ]*@[^@]*\.[^@ ]*/[email protected]/g" file

applied on the following file

[email protected] has an address
so has [email protected]
and finally [email protected] and other stuff

The command C:\Temp>d:sed "s/[^@ ]*@[^@]*\.[^@ ]*/[email protected]/g" file gets me this:

[email protected] has an address
so has [email protected]
and finally [email protected] and other stuff
like image 38
Carsten Massmann Avatar answered Nov 15 '22 04:11

Carsten Massmann


This obfuscates email-adresses partially:

sed -E 's/[a-zA-Z0-9_][a-zA-Z0-9_][a-zA-Z0-9_][a-zA-Z0-9_]@[a-zA-Z0-9_][a-zA-Z0-9_]/xxxx@xx/g'

I found this easier to develop with. Automated tests can now test for specific email adresses.

like image 44
coderofsalvation Avatar answered Nov 15 '22 03:11

coderofsalvation