LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 04-20-2009, 10:41 AM   #1
viniciusandre
LQ Newbie
 
Registered: Oct 2008
Posts: 3

Rep: Reputation: 0
Remove sections of a xml file with sed


I've been trying to remove some lines of a xml file that looks like this:

Code:
<parent>
   <child>name1</child>
   <lots_of_other tags></lots_of_other_tags>
</parent>
<parent>
   <child>name2</child>
   <lots_of_other tags></lots_of_other_tags>
</parent>
<parent>
   <child>name3</child>
   <lots_of_other tags></lots_of_other_tags>
</parent>
How can I remove the '<parent>' to '</parent>' section for 'name2' only?
Thanks in advance.
 
Old 04-20-2009, 11:11 AM   #2
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,189

Rep: Reputation: 105Reputation: 105
Interesting. I would have said sed can't do that, but I found http://www.grymoire.com/Unix/Sed.html#uh-47, which indicates that sed can deal with multi-line patterns. You'll have to read through that and digest it to figure out how to do it.

Alternatively, you would switch to awk or perl, depending on your own preferences.
 
Old 04-20-2009, 01:18 PM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Code:
sed -n '/<parent>/,/<\/parent>/{ H
                                 /<\/parent>/{ s/.*//;x
                                               /name2/d
                                               p
                                             }
                               }' testfile
<parent>
   <child>name1</child>
   <lots_of_other tags></lots_of_other_tags>
</parent>

<parent>
   <child>name3</child>
   <lots_of_other tags></lots_of_other_tags>
</parent>
May need more testing and there are probably better ways of doing it.
The first line uses a range between two parent tags (inclusive range). The `H' command appends the line to the Hold buffer.
The second line tests whether the line read in has the closing tag. If it does, the line buffer is cleared and swapped with the Hold buffer.

At this point, the regular buffer has the entire range in it with the `\n' character between lines.
the third line tests whether it contains `name2'. If so, it is deleted. If not, it is printed.

Last edited by jschiwal; 04-21-2009 at 04:24 AM. Reason: Tightened up indentation
 
  


Reply

Tags
section, sed, xml


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
[Grep,Awk,Sed]Parsing text between XML tags. ////// Programming 5 07-26-2011 11:54 AM
using sed to remove line in a comma-delimited file seefor Programming 4 03-10-2009 03:35 PM
Shell Script or perl help. to write sections of a log to a tmp file for mailing pobman Programming 2 02-02-2009 03:30 PM
PHP Remove Line From Shoutcast Playlist File. Sed? windisch Programming 2 03-26-2007 08:52 PM
how to delete duplicates entries in xml file using sed/awk/sort ? catzilla Linux - Software 1 10-28-2005 02:57 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration