LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 11-21-2008, 04:14 AM   #1
dipuasks
Member
 
Registered: Oct 2005
Location: India
Distribution: Redhat 7-9,Fedora Core 3 - 9, RHEL 4 -5, CentOS 4 - 5, Ubuntu 7.10 - 12.10, Mandirva 2008 -2009
Posts: 133

Rep: Reputation: 16
Replacing certain parts of a string


Hi,

I wrote the below script which replaces all email ids of a certain domain to a fixed email id in all pages of docroot of websites in my webserver recursively. There are multiple accounts(websites) hosted on the server.


for u in $(cat /etc/passwd |grep /home | cut -d : -f1)
do
echo "Now searching & replacing email ids inside account $u ..."
for k in $(grep -rl @mydomain.com /home/$u/public_html/)
do
sed -i s/.*@mydomain.com/myforms1@gmail.com/g $k
done
echo "Completed for account $u "
done


This worked very well, but the problem is when it encountered a string like

$mail2->AddBCC("forms@mydomain.com");

it replaced the whole string with myforms1@gmail.com

Is there a way how to get rid of this?



Thanks in advance.
 
Old 11-21-2008, 04:20 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,443

Rep: Reputation: 4203Reputation: 4203Reputation: 4203Reputation: 4203Reputation: 4203Reputation: 4203Reputation: 4203Reputation: 4203Reputation: 4203Reputation: 4203Reputation: 4203
It did as you specified - make the test more specific. ".*" at the start of a regex like that says "everything up to ...".
Better think some more about the structure of your data, and what you really want to do with it.
 
Old 11-21-2008, 04:33 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
It did exactly what you told it!!

sed 's/.*etc/newstuff/'
means: match any number of characters, followed by "etc", and replace with "newstuff". So it replaced the whole line.

You need an expression which will match the specific patterns found in your file. In your example, you show the address in quotes. For that, you could use:
sed 's/".*@mydomain.com"/newstuff/'
 
Old 11-21-2008, 05:08 AM   #4
dipuasks
Member
 
Registered: Oct 2005
Location: India
Distribution: Redhat 7-9,Fedora Core 3 - 9, RHEL 4 -5, CentOS 4 - 5, Ubuntu 7.10 - 12.10, Mandirva 2008 -2009
Posts: 133

Original Poster
Rep: Reputation: 16
Well I changed the script to:

sed -i 's/".*@mydomain.com"/"myforms1@gmail.com"/g' $k

And it works great, Thanks!

But this will not change simple sentences like

email = abc@mydomain.com

which is again a challenge for me. How to accomplish both at a time? Should I use two SEDs in the script?
 
Old 11-21-2008, 05:56 AM   #5
dipuasks
Member
 
Registered: Oct 2005
Location: India
Distribution: Redhat 7-9,Fedora Core 3 - 9, RHEL 4 -5, CentOS 4 - 5, Ubuntu 7.10 - 12.10, Mandirva 2008 -2009
Posts: 133

Original Poster
Rep: Reputation: 16
Also one more thing, I need to exclude 2 email ids from getting changed. Say for example test@mydomain.com & pass@mydomain.com

Is there any option in SED for the same?

Last edited by dipuasks; 11-21-2008 at 06:06 AM.
 
Old 11-22-2008, 07:09 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
What you need to do is look at all the possible formats for an e-mail address and all the content to be used for making decisions. Sometimes you will need multiple commands, and sometimes you will need something like a sed script with multiple steps.

At this point, I recommend that you read thru the tutorials here: http://www.grymoire.com/Unix/
Look especially at SED, AWK, and Regular Expressions.
Don't try to memorize all the syntax, but just read to get a flavor of the kinds of things that are possible.

I'm still an amateur at scripting, and the only way I get things to work is old-fashioned "cut and try".
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ : replacing a char in a string cb951303 Programming 20 02-25-2015 08:18 PM
chopping away unwanted parts in string C's Brother Programming 3 11-02-2008 07:56 AM
replacing string within same file tostay2003 Programming 2 07-06-2007 02:25 AM
complete string conversion - without parts evaluation slackie1000 Programming 9 01-06-2004 01:10 PM
grep - finding string and replacing with new ckibler Linux - Newbie 6 08-01-2003 06:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 07:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration