If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Extracting blocks from XML file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-27-04, 04:23
corrchris corrchris is offline
Registered User
 
Join Date: Jul 2004
Posts: 3
Extracting blocks from XML file

I have a large XML file and I want to extract blocks from it.

For example, there is a large block:

<tag>
....
</tag>

I want to check if there is a specific string within the block - if yes, then extract the entire block from <tag> to </tag>.

I tried using nawk but I didnt know how to make the "if" part. It's a Sun machine, thus Sun OS.
Reply With Quote
  #2 (permalink)  
Old 07-27-04, 05:01
iaguigon iaguigon is offline
Registered User
 
Join Date: May 2004
Location: Barcelona, Spain
Posts: 54
I do it this way:

:label
/<tag>/,/<\/tag>/ {
/<\/tag>/! {
N;
b label
}
p
}

put the above in a file, say tag.sed and run:

cat xmlfile.xml | sed -n -f tag.sed > outputfile

Regards
Reply With Quote
  #3 (permalink)  
Old 07-27-04, 05:06
corrchris corrchris is offline
Registered User
 
Join Date: Jul 2004
Posts: 3
Thanks, just one question: Where is the "wanted string" ?

Also I got this when I ran the above:

Unrecognized command: /<\/tag>/! {
Broken Pipe
Reply With Quote
  #4 (permalink)  
Old 07-27-04, 05:31
iaguigon iaguigon is offline
Registered User
 
Join Date: May 2004
Location: Barcelona, Spain
Posts: 54
Quote:
Originally Posted by corrchris
Thanks, just one question: Where is the "wanted string" ?

Also I got this when I ran the above:

Unrecognized command: /<\/tag>/! {
Broken Pipe
I am running gnu sed v3.02, maybe it's a sed version issue or a type mismatch? because it works for me.

Instead of just 'p' on line 7 of the tag.sed file it should read
/wanted-string/p

regards
Reply With Quote
  #5 (permalink)  
Old 07-27-04, 05:51
corrchris corrchris is offline
Registered User
 
Join Date: Jul 2004
Posts: 3
I don't know it still doesn't work and I checked it etc.

I'm using Sun OS 5.8 which has its own pecularities. Any chance you could do the same with Sun's sed?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On