The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
GREP and Retrieve Line by Line maxmave Shell Programming and Scripting 2 06-03-2008 08:45 AM
grep a string from 2 text files karthikn7974 Shell Programming and Scripting 15 04-24-2008 12:15 AM
getting particular text after grep from lookup file napolayan UNIX for Dummies Questions & Answers 10 10-20-2006 07:52 AM
grep multiple text files in folder into 1 text file? coppertone UNIX for Dummies Questions & Answers 7 08-23-2002 11:50 AM
grep'ing for text within a bunch of files...? kitykity UNIX for Dummies Questions & Answers 1 09-19-2000 05:50 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-16-2006
Registered User
 

Join Date: Aug 2003
Location: Ireland
Posts: 269
retrieve text after grep

I am trying to search for a pattern in a file containing xml - When I match the search I want to retrieve all the text within the xml brackets..
Whats the best way to read in data between xml tags in a shell script?

ie..
xml returned which I have in a file now is something like below:
<total>
<mainacc>5500</mainacc>
<BalAccs>

<BalAcc>
<AccId>1</AccId>
<Value>0</Value>
</BalAcc>

<BalAcc>
<AccId>2</AccId>
<Value>0</Value>
</BalAcc>

</BalAccs>
</total>

I need to find the section for mainacc and store that but also in the Balaccs section I need to find the information for the value fields in Accid 1 and 2 but also know that the value belongs to accid1 and accid2 ...


Any help??
I have never parsed xml before and not sure where to start ..
Reply With Quote
Forum Sponsor
  #2  
Old 12-16-2006
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
There's almost certainly a more efficient/elegant way of doing this, but the following works for me:

Code:
$ cat ./foo.awk
{
         if ( $0 ~ /<mainacc>/ ) {
            sub( /<mainacc>/, "", $0 )
            sub( /<\/mainacc>/, "", $0 )
            mainacc=$0
            printf( "mainacc: %s\n", $0 )
         }
         if ( $0 ~ /<AccId>/ ) {
            sub( /<AccId>/, "", $0 )
            sub( /<\/AccId>/, "", $0 )
            accid=$0
            printf( "\tAccId: %s", $0 )
         }
         if ( $0 ~ /<Value>/ ) {
            sub( /<Value>/, "", $0 )
            sub( /<\/Value>/, "", $0 )
            value=$0
            printf( "\tValue: %s\n", $0 )
         }
}
$ awk -f ./foo.awk infile
mainacc: 5500
        AccId: 1        Value: 0
        AccId: 2        Value: 0
mainacc: 5501
        AccId: 1        Value: 55555
        AccId: 2        Value: 6
mainacc: 5505
        AccId: 1        Value: 10
        AccId: 2        Value: 100
Cheers
ZB
Reply With Quote
  #3  
Old 12-16-2006
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
Setting the awk field separator may help, e.g....
Code:
$ awk -F '[<>]' '/(mainacc|AccId)/ {print $2,$3}' file1
mainacc 5500
AccId 1
AccId 2
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0