Register
It is currently Wed Apr 23, 2014 11:33 am

search and replace line using variables.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Wed Jun 29, 2011 6:29 pm   

Joined: Wed Jun 29, 2011 6:14 pm
Posts: 1
I'm trying to search a xml file, find a line containing a string and if the line is not a comment, replace a line with a new line which is a variable.

code:
WIN_IMAGE='<general image="c:\program files\adacel\sysman\client\Sysman.jpg"/>'

# Modify the image file location
sed '/<!--/!s/<general image=*/$WIN_IMAGE/g' $TMP_DIR/"Sysman.xml" > $TMP_DIR/"1.xml"

The result is a line containing the literal characters "$WIN_IMAGE" instead of the string in the variable.

What i'm I doing wrong?

Thanks


Top
 Profile  
 PostPosted: Wed Jun 29, 2011 9:00 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
zamboni128 wrote:
I'm trying to search a xml file...


Where are you doing it in your code?

zamboni128 wrote:
...find a line containing a string...


Again, where is this task?

------

So there are two issues in your sed(1) line.

The first one is that you want to expand the variable WIN_IMAGE by its value,
using the dollar sign, within single quotes, whereas expansion occurs within double quotes.

The second is that the variable WIN_IMAGE contains the "/" character, so sed(1) will treat it as one of its delimiters.
The solution to this issue is to use another delimiter, like a comma, for example.

Also, I'd use the "${...}" syntax to make the command more readable, and use lowercase variables because they aren't part of the shell environment.

Code:
sed "/<!--/! s,<general image=.*,${win_image},g" "$tmp_dir"/Sysman.xml > "$tmp_dir"/1.xml


Note that this sed(1) command won't work if you're in an interactive shell with history expansion enabled.
To disable it, run set +H. ( « help set » )


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP