LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux > 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

Tags used in this thread
Popular LQ Tags , ,

Reply
 
Thread Tools
Old 04-20-2009, 11:41 AM   #1
viniciusandre
LQ Newbie
 
Registered: Oct 2008
Posts: 3
Thanked: 0
Remove sections of a xml file with sed


[Log in to get rid of this advertisement]
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.
viniciusandre is offline  
Tag This Post , ,
Reply With Quote
Old 04-20-2009, 12:11 PM   #2
choogendyk
Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 854
Thanked: 34
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.
choogendyk is offline     Reply With Quote
Old 04-20-2009, 02:18 PM   #3
jschiwal
Moderator
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 12,881
Thanked: 232
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
jschiwal is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
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
[Grep,Awk,Sed]Parsing text between XML tags. ////// Programming 3 01-20-2009 03:49 AM
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


All times are GMT -5. The time now is 09:00 PM.

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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration