Remove sections of a xml file with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove sections of a xml file with sed
# 1  
Old 04-20-2009
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.
# 2  
Old 04-20-2009

If, by remove, you mean to extract only that section:

Code:
awk -v RS='</parent>' 'NR == 2 {print $0 RS } '

If you mean to select everything but that section:

Code:
awk -v RS='</parent>' 'NR != 2 {print $0 RS } '

That is a little buggy, but it should give you enough to work with.
# 3  
Old 04-20-2009
That was cool...
This awk command wraps everything inside the <parent> tags:

Code:
awk -v RS='</parent>' 'NR == 2 {print $0 RS } '

But I don't want to print just the second wrap, but the wrap that matches "name2" on it.

How can I do this?
Thanks again!
# 4  
Old 04-20-2009
[indent]
Code:
awk -v RS='</parent>' '/name2/ {print $0 RS } '

[indent]
# 5  
Old 04-20-2009
Ok! Good! And what if I want to print everything but this? Is there a '!' pattern to put somewhere?
# 6  
Old 04-20-2009
Got it, nevermind. I was putting it before the '. The correct is '!.
Thanks by now!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove sections based on duplicate first line

Hi, I have a file with many sections in it. Each section is separated by a blank line. The first line of each section would determine if the section is duplicate or not. if the section is duplicate then remove the entire section from the file. below is the example of input and output.... (5 Replies)
Discussion started by: ahmedwaseem2000
5 Replies

2. UNIX for Dummies Questions & Answers

[Solved] How remove leading whitespace from xml (sed /awk?)

Hi again I have an xml file and want to remove the leading white space as it causes me issues later in my script I see sed is possible but cant seem to get it to work I tried sed 's/^ *//' file.xml output <xn:VsDataContainer id="1U104799" modifier="update"> ... (10 Replies)
Discussion started by: aniquebmx
10 Replies

3. Shell Programming and Scripting

How to remove sections of a filename?

Hello, I need some help with renaming some files by removing a certain portion of the filename. The current file name is: ABC_2013186197_20130708_203556.95336 I need to remove the 5 digits after the first "_". The new file name should be: ABC_197_20130708_203556.95336 I'm not quite... (5 Replies)
Discussion started by: bbbngowc
5 Replies

4. Shell Programming and Scripting

Remove lines with non-chinese characters from xml file

Hi there, I'm looking for a way to remove all lines that don't contain chinese characters from an xml file. Example: http://pastebin.com/8KzSbCKe The result should be like this: http://pastebin.com/ZywXsNhx Only lines that don't contain chinese characters should be deleted. If theres a mix of... (3 Replies)
Discussion started by: g4rb4g3
3 Replies

5. Shell Programming and Scripting

remove XML parent start and end tags in same file

Hi All, Requirement: remove start and end tag of parent element <DummyLevel> <level1> </level1> <level2> </level2> <level3> </level3> <level4> </level4> <level5> </level5> <level6> </level7> </DummyLevel> I have to delete the first <dummylevel> and last </DummyLevel> tags from... (7 Replies)
Discussion started by: dstage2006
7 Replies

6. Shell Programming and Scripting

SED 4.1.4 - INI File Change Problem in Variables= in Specific [Sections] (Guru Help)

GNU sed version 4.1.4 on Windows XP SP3 from GnuWin32 I think that I've come across a seemingly simple text file change problem on a INI formatted file that I can't do with SED without side effects edge cases biting me. I've tried to think of various ways of doing this elegantly and quickly... (5 Replies)
Discussion started by: JakFrost
5 Replies

7. Shell Programming and Scripting

How to remove xml namespace from xml file using shell script?

I have an xml file: <AutoData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Table1> <Data1 10 </Data1> <Data2 20 </Data2> <Data3 40 </Data3> <Table1> </AutoData> and I have to remove the portion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" only. I tried using sed... (10 Replies)
Discussion started by: Gary1978
10 Replies

8. Shell Programming and Scripting

Best way to remove sections of text from a file

Greetings! I found this fourm via a google search on "sed expressions". I have a file that contains notices and they are all the same length in lines. For example the file would contains 15 notices, each being 26 lines each. I need some way to eliminate notices that contain a "S" in a particular... (8 Replies)
Discussion started by: cals64
8 Replies

9. Shell Programming and Scripting

Remove xmlns from xml output file

Hi, I have the xml output file which contents the namespace xmlns. I would like to remove that line <AutoData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Table1> <Data1 10 </Data1> <Data2 20 </Data2> <Data3 40 </Data3> <Table1> </AutoData> I would like to remove line... (1 Reply)
Discussion started by: ca_sr2274
1 Replies

10. Shell Programming and Scripting

remove duplicated xml record in a file under unix

Hi, If i have a file with xml format, i would like to remove duplicated records and save to a new file. Is it possible...to write script to do it? (8 Replies)
Discussion started by: happyv
8 Replies
Login or Register to Ask a Question