LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-17-2011, 11:33 AM   #1
jomann
LQ Newbie
 
Registered: Mar 2011
Posts: 24

Rep: Reputation: 0
sed doesn't accept variables to replace in file


Hi there,

I'm really getting crazy to get the following done (linux):

Code:
sed -e "s/$old/$new/g" $origfile > $newfile
mv $newfile $origfile
The variables are clean and available in my script. No prob.

$old is an IP-address of a server, $new as well. replacement-file is an xml-file (the ip-address to be changed is within an xml-tag).

It should be trivial, but it isn't. I spent the last 3 hours reading about the problem, trying several different 'solutions' or workarounds.
It should work, the '"' should take care that the variables get expanded, but whatever I try the old server IP isn't replaced.

Plz, I need help. Any idea, anybody?
Regards,
jomann.
 
Old 03-17-2011, 11:42 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Hmm, that looks like it should work. Try it with the following and see if it helps:
Code:
sed -e s/"$old"/"$new"/g $origfile > $newfile
It also depends on what the variables contain.....
 
Old 03-17-2011, 12:07 PM   #3
jomann
LQ Newbie
 
Registered: Mar 2011
Posts: 24

Original Poster
Rep: Reputation: 0
Hi, thx a lot.

I already tried this one, too. but the double quotes eclosing the variables don't help. This doesn't work either.

Maybe some more info helps:

I get my variable from another file like this:
Code:
new=$(grep srvadr $conf | awk '{print $2}')
It's a .conf with a line like this:
srvadr 192.168.0.1

The .xml-file ($origfile, which gets the replacement) looks like this:
<DefaultDirectory value="smb://192.168.137.1/sharename" />

If I change the sed-cmd to:
Code:
sed -e "s/$old/abc/g" $origfile > $newfile
or
Code:
sed -e s/"$old"/"abc"/g $origfile > $newfile
it works perfect.

Regards, jomann.
 
Old 03-17-2011, 12:16 PM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,466

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Hi

Not sure, but try to run your script with:

bash -x scriptname

Then you see what's being sent to sed.
 
Old 03-17-2011, 12:17 PM   #5
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
I think this is something simple.... Try the following:
Code:
sed -e "s/"$old"/"$new"/g" $origfile > $newfile
 
Old 03-17-2011, 12:39 PM   #6
jomann
LQ Newbie
 
Registered: Mar 2011
Posts: 24

Original Poster
Rep: Reputation: 0
I always thought so, too: it must be something simple (but after 3 hours staring at the code I got malfunction in my eyes & brain).

And indead it is... It works with the double quotes as it should:
Code:
sed -e s/"$old"/"$new"/g $origfile > $newfile
The bash debug-mode revealed that I mixed up two IP addresses (I have some more variables), therefore my script was searching to replace something that wasn't there.

Thanks very much for your help!
jomann.
 
Old 03-17-2011, 12:40 PM   #7
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
LOL Nice! Well glad you got it resolved. Mark your thread as SOLVED and give rep if you want to, thanks!

Josh
 
Old 03-18-2011, 05:16 AM   #8
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
NB: There is the switch -i for in place substitution, so you could save the mv.
 
  


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
[SOLVED] sed command to replace file extension leighya Linux - Newbie 18 04-22-2012 03:20 AM
[SOLVED] Using File as Replace in Sed abercrombieande Programming 9 03-04-2011 02:55 PM
[SOLVED] Using Sed to search and replace does not accept spaces? timdvtemp Linux - Software 5 02-16-2011 04:19 PM
search and replace predefined variables in a file adam_blackice Programming 7 04-14-2008 08:54 PM
[SOLVED] Replacing using sed with variables from another file? eamesj Programming 5 11-24-2006 10:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:26 AM.

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