![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 .. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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
ZB |
|
#3
|
||||
|
||||
|
Setting the awk field separator may help, e.g....
Code:
$ awk -F '[<>]' '/(mainacc|AccId)/ {print $2,$3}' file1
mainacc 5500
AccId 1
AccId 2
|
||||
| Google The UNIX and Linux Forums |