LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-03-2007, 12:18 PM   #1
jakev383
QmailToaster Developer
 
Registered: Dec 2005
Location: Burlington, NC
Distribution: CentOS, Voyage, Debian, Fedora
Posts: 220

Rep: Reputation: 31
sed question for search and replace


I'm running into a problem where I don't see what I'm doing wrong. I have a line in a config file (it will always be line 38, FWIW):
$rcmail_config['default_host'] = '';

That I want to replace with:

$rcmail_config['default_host'] = 'localhost';

No problem. Trusty sed should get the job done, right?

sed -e 's/$rcmail_config[\'default_host\'] = \'\';/$rcmail_config[\'default_host\'] = \'localhost\';/g' main.config

But I think I have my escapes wrong somewhere.
Can someone tell me where I messed up?
Thanks in advance.
 
Old 05-03-2007, 12:30 PM   #2
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
It might be because of this: The poorly undocumented ;

HTH,

Centinul
 
Old 05-03-2007, 01:34 PM   #3
jakev383
QmailToaster Developer
 
Registered: Dec 2005
Location: Burlington, NC
Distribution: CentOS, Voyage, Debian, Fedora
Posts: 220

Original Poster
Rep: Reputation: 31
I'm sure it's because I'm not escaping something in there.... SO I tried this:

sed -e 's/$rcmail_config[\'default_host\'] = \'\'\;/$rcmail_config[\'default_host\'] = \'localhost\'\;/g' main.config

And that gives me a line like it's waiting for something else.
 
Old 05-03-2007, 01:40 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

If the line is always on line 38:

sed "38s/'';$/'localhost';/" infile

If it's not:

sed "s/\$rcmail_config\['default_host'\] = '';$/\$rcmail_config['default_host'] = 'localhost';/" infile

Hope this helps.
 
Old 05-03-2007, 04:42 PM   #5
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
Druuna
Are you able to write those sed commands without looking at a book or some other material?
I know you are clever.
I must look at a book or online material to write sed and awk commands.
 
Old 05-03-2007, 07:26 PM   #6
jakev383
QmailToaster Developer
 
Registered: Dec 2005
Location: Burlington, NC
Distribution: CentOS, Voyage, Debian, Fedora
Posts: 220

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by druuna
Hi,

If the line is always on line 38:

sed "38s/'';$/'localhost';/" infile

If it's not:

sed "s/\$rcmail_config\['default_host'\] = '';$/\$rcmail_config['default_host'] = 'localhost';/" infile

Hope this helps.
Grr.... Neither of those work either.
And yes, it will always be on line 38.
This did work, though:

perl -pi -e "s/''/'localhost'/" main.inc.php.dist

Thanks for all the help. The double quotes got me bumped in the right direction!
 
Old 05-04-2007, 12:27 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

perl, where did that come from? You asked for a sed solution

Maybe you just settled for perl. It's strange that the two examples given don't seem to work (you remember what went wrong?). I just tried both of them and here they do what they are supposed to do.

But, you have a working solution, thats all that matters in the end

@Gins: By now I don't need books/the net for this, but that's experience.

EDIT
@jakev383: You do know that the " after sed and before infile are double qoutes, not 2 single quotes? The '' after the / and before the ; are 2 single quotes.
/EDIT

Last edited by druuna; 05-04-2007 at 12:33 AM.
 
Old 05-04-2007, 04:20 AM   #8
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
Druuna
When it comes to sed and awk, there are hundreds or rather thousands of commands. You should be a genius to know everything by heart. I am an idiot.

When it comes to 'grep' commands, there is a limit. I may be wrong.
 
Old 05-05-2007, 05:40 AM   #9
jakev383
QmailToaster Developer
 
Registered: Dec 2005
Location: Burlington, NC
Distribution: CentOS, Voyage, Debian, Fedora
Posts: 220

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by druuna
Hi,

perl, where did that come from? You asked for a sed solution

Maybe you just settled for perl. It's strange that the two examples given don't seem to work (you remember what went wrong?). I just tried both of them and here they do what they are supposed to do.

But, you have a working solution, thats all that matters in the end

@Gins: By now I don't need books/the net for this, but that's experience.

EDIT
@jakev383: You do know that the " after sed and before infile are double qoutes, not 2 single quotes? The '' after the / and before the ; are 2 single quotes.
/EDIT
Where did perl come from? Once thing I've learned in Linux - it doesn't matter what you do or how you do it, as long as 2+2=4 in the end. If nothing else I think it's safe to say there's at least 3 ways of doing anything in Linux.
That being said, when I was doing a search-n-replace sed popped in my head. I couldn't get it to do what I wanted and after Gins bumped me with those double quotes, well, sed still didn't work but I thought "why not try perl?" since I was using it 5 lines prior in my little script anyway.
And yeah, I don't know why those don't work either. I ran them on a Cent4.4 machine which shouldn't matter but I've seen some weird things on Debian distros where /bin/sh wasn't linked to /bin/bash so scripts did funny things sometimes.
Thanks everyone!
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bit complicated sed search & replace sharathkv25 Programming 6 04-07-2007 01:53 PM
sed search & replace sharathkv25 Programming 2 03-07-2007 10:16 AM
another question about search and replace text graziano1968 Linux - General 3 08-02-2006 09:35 AM
regular expression search/replace question (HELP!!! :) amytys Programming 5 09-06-2004 02:36 PM
sed or awk question - replace caps with small letters computera Linux - General 1 12-30-2003 04:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:16 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