Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Software
User Name
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.


  Search this Thread
Old 04-20-2009, 10:41 AM   #1
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:

   <lots_of_other tags></lots_of_other_tags>
   <lots_of_other tags></lots_of_other_tags>
   <lots_of_other tags></lots_of_other_tags>
How can I remove the '<parent>' to '</parent>' section for 'name2' only?
Thanks in advance.
Old 04-20-2009, 11:11 AM   #2
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, 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
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681
sed -n '/<parent>/,/<\/parent>/{ H
                                 /<\/parent>/{ s/.*//;x
                               }' testfile
   <lots_of_other tags></lots_of_other_tags>

   <lots_of_other tags></lots_of_other_tags>
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


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 > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:05 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration