LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 07-07-2005, 04:36 AM   #1
marri
Member
 
Registered: May 2004
Posts: 38

Rep: Reputation: 15
Replace substring with SED


hi
I'm trying to figure out how I could replace part of the XML schema I have here below using SED:

Code:
<release>
<supported>
<machine> 213.220.81.240
<type> 103
<ipboot> 225.1.10.1:1103
<ipupgrade> 225.1.10.1:2103
</type>
</machine>
</supported>
<server> 213.220.81.240
<dhcp> 213.220.81.240
<model> 103
<version_active> 0.8.0
<DI> 12
<dir> /test/103/0.8.0
<date> Thu-Jan-20-16:36:51-GMT-2005
</model>
</server>
</release>
I'm trying to use SED to replace the <model> - </model> part with another data, without to much effort. I know I can do this by copying parts to a temp file and play around with in such a way but I was wondering if I could do this with a single line using SED.

Example of <model> - </model> replacement could be:
Code:
<model> 110
<version_active> 0.8.2
<DI> 14
<dir> /test/110/0.8.2
<date> Thu-Jul-07-16:36:51-GMT-2005
</model>
regards
Marri
 
Old 07-08-2005, 01:44 PM   #2
Chrax
Member
 
Registered: Apr 2004
Distribution: Dapper
Posts: 167

Rep: Reputation: 31
I don't understand exactly what you're trying to do. Where is the source of the changes you want to make. In this example, how should we know that you want to change
Code:
<model> 103
to
Code:
<model> 110
?

Or do you mean something like:

Code:
sed -i "s/<model> 103/<model> 110/"
but extended to perform the entire switch? If that's the case, sed is hardly an excellent way to do this, as it's just as easy to change the file by hand (if not more so, as you don't have to worry about sed syntax).
 
Old 07-09-2005, 05:18 PM   #3
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Use awk. When you find the first pattern (<model>) set a flag that you can test to skip/replace the following lines. When you find the second pattern (</model>) unset the flag. Something like:
Code:
awk '{/a == 1/	{ next }
/<\/model>/	{a=0}
/<model/		{a=1}'  filename
The syntax may need work, I haven't used awk for a while.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
replace a substring with another string in C zeppelin Programming 21 11-09-2009 09:59 PM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 10:58 AM
Using sed to replace a lot of text Wynd Programming 9 10-07-2005 03:47 AM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM
Sed - suitable to replace CR LF? J_Szucs Programming 3 05-12-2003 06:03 PM

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

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