Check the value in xml based on condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check the value in xml based on condition
# 1  
Old 05-07-2013
Check the value in xml based on condition

Hi,

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

Code:
<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.
Code:
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.

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

Regards
Neethu
# 2  
Old 05-07-2013
use this command
Code:
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 !
Code:
 awk -F"<oID>|</oID>|<status>|</status>"  '{if( $2 == "A123") {print "oID:"$2" status:"$4} else print "oID not maching" }' file

This User Gave Thanks to Revansidhu For This Post:
# 3  
Old 05-07-2013
Thanks a lot. It is working fine..
# 4  
Old 05-07-2013
Another approach:
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with tag value extraction from xml file based on a matching condition

Hi , I have a situation where I need to search an xml file for the presence of a tag <FollowOnFrom> and also , presence of partial part of the following tag <ContractRequest _LoadId and if these 2 exist ,then extract the value from the following tag <_LocalId> which is "CW2094139". There... (2 Replies)
Discussion started by: paul1234
2 Replies

2. Shell Programming and Scripting

Help with XML tag value extraction based on condition

sample xml file part <?xml version="1.0" encoding="UTF-8"?><ContractWorkspace xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" _LoadId="export_AJ6iAFmh+pQHq1" xsi:noNamespaceSchemaLocation="ContractWorkspace.xsd"> <_LocalId>CW2218471</_LocalId> <Active>true</Active> ... (3 Replies)
Discussion started by: paul1234
3 Replies

3. Shell Programming and Scripting

Help with XML tag value extraction based on matching condition

sample xml file part <DocumentMinorVersion>0</DocumentMinorVersion> <DocumentVersion>1</DocumentVersion> <EffectiveDate>2017-05-30T00:00:00Z</EffectiveDate> <FollowOnFrom> <ContractRequest _LoadId="export_AJ6iAFoh6g0rE9"> <_LocalId>CRW2218451</_LocalId> ... (4 Replies)
Discussion started by: paul1234
4 Replies

4. Shell Programming and Scripting

Split xml file into multiple xml based on letterID

Hi All, We need to split a large xml into multiple valid xml with same header(2lines) and footer(last line) for N number of letterId. In the example below we have first 2 lines as header and last line as footer.(They need to be in each split xml file) Header: <?xml version="1.0"... (5 Replies)
Discussion started by: vx04
5 Replies

5. Shell Programming and Scripting

Check first column - average second column based on a condition

Hi, My input file Gene1 1 Gene1 2 Gene1 3 Gene1 0 Gene2 0 Gene2 0 Gene2 4 Gene2 8 Gene3 9 Gene3 9 Gene4 0 Condition: If the first column matches, then look in the second column. If there is a value of zero in the second column, then don't consider that record while averaging. ... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

6. Shell Programming and Scripting

Replace Char in XML Base on Condition

HI All Here is my Input file A. I want to add extra char in the line base on below condition. 1> if Below second line is <xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType> then <xn:VsDataContainer id= will be <xn:VsDataContainer_id_1= 2 > if Below second line is... (4 Replies)
Discussion started by: asavaliya
4 Replies

7. UNIX for Dummies Questions & Answers

Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)

Below is the sample logfile: Userids Date Time acb Checkout time: 2013-11-20 17:00 axy Checkout time: 2013-11-22 12:00 der Checkout time: 2013-11-17 17:00 xyz Checkout time: 2013-11-19 16:00 ddd Checkout time: 2013-11-21 16:00 aaa Checkout... (9 Replies)
Discussion started by: asjaiswal
9 Replies

8. Shell Programming and Scripting

Replace text inside XML file based on condition

Hi All, I want to change the name as SEQ_13 ie., <Property Name="Name">SEQ_13</Property> when the Stage Type is PxSequentialFile ie., <Property Name="StageType">PxSequentialFile</Property> :wall: Input.XML <Main> <Record Identifier="V0S13" Type="CustomStage" Readonly="0">... (3 Replies)
Discussion started by: kmsekhar
3 Replies

9. Shell Programming and Scripting

extract xml tag based on condition

Hi All, I have a large xml file of invoices. The file looks like below: <INVOICES> <INVOICE> <NAME>Customer A</NAME> <INVOICE_NO>1234</INVOICE_NO> </INVOICE> <INVOICE> <NAME>Customer A</NAME> <INVOICE_NO>2345</INVOICE_NO> </INVOICE> <INVOICE> <NAME>Customer A</NAME>... (9 Replies)
Discussion started by: angshuman
9 Replies

10. Shell Programming and Scripting

Remove lines from XML based on condition

Hi, I need to remove some lines from an XML file is the value within a tag is empty. Imagine this scenario, <acd><acdID>2</acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> I... (3 Replies)
Discussion started by: giles.cardew
3 Replies
Login or Register to Ask a Question