Linux - Server This forum is for the discussion of Linux Software used in a server related context. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-21-2008, 04:14 AM
|
#1
|
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:
|
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.
|
|
|
11-21-2008, 04:20 AM
|
#2
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,443
|
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.
|
|
|
11-21-2008, 04:33 AM
|
#3
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
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/'
|
|
|
11-21-2008, 05:08 AM
|
#4
|
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:
|
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?
|
|
|
11-21-2008, 05:56 AM
|
#5
|
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:
|
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.
|
|
|
11-22-2008, 07:09 AM
|
#6
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
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".
|
|
|
All times are GMT -5. The time now is 07:36 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|