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