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, 11: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, 12:11 PM   #2
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

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, 02:18 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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 05: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 12:54 PM
using sed to remove line in a comma-delimited file seefor Programming 4 03-10-2009 04: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 04:30 PM
PHP Remove Line From Shoutcast Playlist File. Sed? windisch Programming 2 03-26-2007 09:52 PM
how to delete duplicates entries in xml file using sed/awk/sort ? catzilla Linux - Software 1 10-28-2005 03:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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