Check the value in xml based on condition

Hi,

I have a log file having n number of xml's like the one below.

<uOStatus xmlns:env="http://abc.org/def/ghi/" xmlns:soapenv="http://abc.def.org/soap/envelope/"><ID>12345</ID><oID>A123</oID><Identifier><aID>ABC</aID><sID>ABCID</sID><sVersion>1</sVersion><timeStamp1>2013-05-07T09:04:05.849</timeStamp1><rID>A123</rID><cID/><iID/></Identifier><oStatus><opStatus><status>NO_ACTION_REQUIRED</status><dc/><olStatus><olID/><olpStatus/><old/></olStatus></opStatus><dc>EC [1] ED [Success]</dc></oStatus><ccs/></uOStatus>

I want to check the oID of the xml's from the log. If the 0ID=A123 then i want to check the value of status.

I tried the below code to pull the xml from the logs.

awk '/<status/ { N++ } N; /<\/status/ { N-- }' log > uOStatuslog

If I use the below code I am able to pull only the oID values but I will not be able to check the value of status for this oID.

perl -lne 'BEGIN{undef $/} while(/(<oID>(.*?)<\/oID>)/sg) {$x=$1; print $x if $2 =~ /A123/}' uOStatuslog > oIDlog

Regards
Neethu

use this command

awk -F"<oID>|</oID>|<status>|</status>"  '{print $2":"$4}' file |  awk -F: '{ if( $1 == "A123") {print "oID:"$1" status:"$2} else print "oID not maching" }'

---------- Post updated at 05:37 AM ---------- Previous update was at 05:35 AM ----------

Or try this one !

 awk -F"<oID>|</oID>|<status>|</status>"  '{if( $2 == "A123") {print "oID:"$2" status:"$4} else print "oID not maching" }' file
1 Like

Thanks a lot. It is working fine..

Another approach:

awk -F'[<>]' '
                {
                        for ( i = 1; i <= NF; i++ )
                        {
                                if ( $i == "oID" )
                                {
                                        if ( $( i + 1 ) == "A123" )
                                        {
                                                print "oID = " $( i + 1 )
                                                f = 1
                                        }
                                }
                                if ( $i == "status" && f )
                                {
                                        print "status = " $( i + 1 )
                                        f = 0
                                }

                        }
                }
' file.xml