retrieve text after grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers retrieve text after grep
# 1  
Old 12-16-2006
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 ..
# 2  
Old 12-16-2006
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
# 3  
Old 12-16-2006
Setting the awk field separator may help, e.g....
Code:
$ awk -F '[<>]' '/(mainacc|AccId)/ {print $2,$3}' file1
mainacc 5500
AccId 1
AccId 2

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep: Retrieve two strings from one file to find them anyone on line in another file

I am having trouble matching *two* strings from one file anywhere in a line of a second file, and could use some help getting this figured out. My preference would be to use grep for this because I would like to take advantage of its -A option. The latter is due to the fact that I would like both... (2 Replies)
Discussion started by: jvoot
2 Replies

2. UNIX for Beginners Questions & Answers

How to search a text in file and retrieve required lines following it with UNIX command?

I have requirement to search for a text in the file and retrieve required lines that is user defined with unix command. Eg: Find the text UNIX in the below file and need to return Test 8 & Test 9 Test 1 Test 2 Test 3 Test 4 UNIX Test 5 Test 6 Test 7 Test 8 Test 9 Result can... (8 Replies)
Discussion started by: Arunkumarsak4
8 Replies

3. Shell Programming and Scripting

Retrieve information Text/Word from HTML code using awk/sed

awk/sed newbie here. I have a HTML file and from that file and I would like to retrieve a text word. <font face=arial size=-1><li><a href=/value_for_clients/Tokyo/abc_process.txt>abc</a> NDK Version: 4.0 </li> <font face=arial size=-1><li><a... (6 Replies)
Discussion started by: sk2code
6 Replies

4. Shell Programming and Scripting

Perl code to retrieve text from website

perl -MLWP::Simple -le '$s=shift;$c=get("http://www.google.com/intl/en/chrome/devices/chromecast/$s/");$c=~/meta content=(.*?)name=\"Remote free\"/msg; print length($1),"\t$1"' ?gclid=CJDg27OdnL0CFcFlOgodFD8A6Q >output.txt output.txt should be: Chromecast works with devices you already own,... (9 Replies)
Discussion started by: cmccabe
9 Replies

5. Shell Programming and Scripting

How can i run sql queries from UNIX shell script and retrieve data into text docs of UNIX?

Please share the doc asap as very urgently required. (1 Reply)
Discussion started by: 24ajay
1 Replies

6. Shell Programming and Scripting

Grep text matching problem with script which checks if web page contains text.

I wrote a Bash script which checks to see if a text string exists on a web page and then sends me an email if it does (or does not e.g. "Out of stock"). I run it from my crontab, it's quite handy from time to time and I've been using it for a few years now. The script uses wget to download an... (6 Replies)
Discussion started by: gencon
6 Replies

7. Shell Programming and Scripting

Retrieve logs generated in last 10 mins from a log file using 'grep' command

HI All, I have a log file where the logs will be in the format as given below: 2011-05-25 02:32:51 INFO PROCESS STARTING 2011-05-25 02:32:52 INFO PROCESS STARTED . . . I want to retrieve only the logs which are less than 5 mins older than current time using grep... (3 Replies)
Discussion started by: rvhg16
3 Replies

8. Shell Programming and Scripting

grep command to retrieve one file

The Sed/Grep command is really confusing me. I know I'm missing something that should be really easy to fix. My program displays multiple names after I ask it to display only one, How do I get it to do only one?? it looks like this: Please enter a name to display? >> John (A list then... (9 Replies)
Discussion started by: toejam
9 Replies

9. UNIX for Dummies Questions & Answers

retrieve lines using sed, grep or awk

Hi, I'm looking for a command to retrieve a block of lines using sed or grep, probably awk if that can do the job. In below example, By searching for words "Third line2" i'm expecting to retrieve the full block starting with 'BEGIN' and ending with 'END' of the search. Example: ... (3 Replies)
Discussion started by: learning_linux
3 Replies

10. UNIX for Dummies Questions & Answers

grep multiple text files in folder into 1 text file?

How do I use the grep command to take mutiple text files in a folder and make one huge text file out of them. I'm using Mac OS X and can not find a text tool that does it so I figured I'd resort to the BSD Unix CLI for a solution... there are 5,300 files that I want to write to one huge file so... (7 Replies)
Discussion started by: coppertone
7 Replies
Login or Register to Ask a Question