tags:

views:

199

answers:

3

Hello all, I have multiple email id's in some config files in a directory; I'm running my scripts on a Solaris machine. I want to perform the following:

Find all the email_id's in the config files in a directory: eg: [email protected] ; [email protected] ; [email protected] ; [email protected]

Replace all existing id's with: [email protected]

The following implementation can help me replace "hotmail" with "gmail" for all the email id's in the config files. But i'm a little confused to solve the above problem

perl -pi -e 's/\@hotmail/\@gmail/g' *

Thanks in advance!

A: 

What you need is an editor with regex/global replace (and make sure it creates *.bak files)

sure, coding this is fun ..

lexu
But the correct regular expression to achieve this would still be needed.
Rahul
I didn't claim it wasn't .. I was pointing out, that writing a program might not be the ideal solution, unless the problem arises often.
lexu
+4  A: 

Try

's/\S+@hotmail\.com/[email protected]/g'
heferav
Fantastic Heferav! excellent soulution.
novice
Pleased it worked. Regex is very powerful if you use it right
heferav
It can also be very powerful if you use it wrong. You should escape the dot so that it doesn't just match any old character.
innaM
@Manni - good point, I amended my answer
heferav
A: 

using the solution posted by heferav, i don't seem to get the answer

$ more file
[email protected] ; [email protected] ; [email protected] ; [email protected]
$ perl -ne 'print if s/\S+@hotmail\.com/[email protected]/g' file
wxyz.com ; wxyz.com ; wxyz.com ; wxyz.com

maybe I am missing something. @OP, since you are working in Solaris, i assume you can use nawk

$ nawk '{ for(i=1;i<=NF;i++){gsub(/.*@hotmail.com/,"wxyz@hotmail",$i)} }1' file
wxyz@hotmail ; wxyz@hotmail ; wxyz@hotmail ; wxyz@hotmail
ghostdog74