Grep some values from XML file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep some values from XML file
# 1  
Old 03-31-2016
Grep some values from XML file

Dear community,
I have a big XML log file containing several rows splitted by tag: <ActivityLogRecord> and </ActivityLogRecord>. An example below.

What I need is read the file and extract some value from each tags and put them into one line (each line for every <ActivityLogRecord> tag).

So in the example the output should be:
Code:
COSID='88',true,Value='393290439266'

The problem where I'm scratching my head is that sometimes this field is missing: <WhoCalledOn>true</WhoCalledOn> and this could be reported twice, something like:
Code:
<Identifier Type='TelephoneNumber' Value='324234231443'/>
<Identifier Type='TelephoneNumber' Value='324234231443'/>

This means the output I need should be:
Code:
COSID='88',true,Value='393290439266',null
COSID='88',null,Value='393290439266',Value='393290439266'

In other words, if the tag WhoCalledOn is missing, I should report in output something like NULL, otherwise put the value like true o false. Same thing for the TelephoneNumber tag. If reported only one, the second column should be set as NULL, otherwise report the value.

Well, I know, this is not properly simple, that's why I'm asking to the expert! Smilie

Thank you
Lucas


Code:
<ActivityLogRecord>
   <Common COSID='88' DomainID='BL' EndTimeStamp='2016-03-23T10:00:00.10+01:00' MainAction='Modify Subscriber' ReferenceNumber='192.9.224.15-1455979927516' ServerID='omu234234' ServiceName='SPM' StartTimeStamp='2016-03-23T09:59:59.850+01:00' UserID='234234234234' UserTerminal='vxv_app_user'>
      <MainActionResult Description='ERROR: 0 - Action completed successfully' Status='Success'/>
   </Common>
   <ServiceSpecific>
      <Provisioning xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
         <Request>
            <Header>
               <Command>Modify</Command>
               <EntityIdentifiers>
                  <Identifier Value='324234231443'/>
               </EntityIdentifiers>
                <Data>
                   <Subscriber>
                      <WhoCalledOn>true</WhoCalledOn>
                   </Subscriber>
                </Data>
               <HostReference>Automatic</HostReference>
            </Header>
            <Data>
               <Subscriber>
                  <SubscriberCosName>8</SubscriberCosName>
                  <SubscriberDomainName>Default domain</SubscriberDomainName>
               </Subscriber>
            </Data>
         </Request>
      </Provisioning>
      <Provisioning xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
         <Response>
            <Header>
               <EntityIdentifiers>
                  <Identifier Type='TelephoneNumber' Value='324234231443'/>
               </EntityIdentifiers>
               <EntityName>Subscriber</EntityName>
               <HostReference>Automatic</HostReference>
               <ResponseStatus>Success</ResponseStatus>
            </Header>
         </Response>
      </Provisioning>
   </ServiceSpecific>
</ActivityLogRecord>

# 2  
Old 03-31-2016
Not sure I understand. Looking at your sample, I can't see WHAT should be reported twice. And, is "COSID=" a constant present in all records that can be searched for, or part of the <common> tag?

Please show us your attempts so far.
# 3  
Old 03-31-2016
Well, first of all thanks for reply, what I need is the 3 red value in my previous example.
As I wrote COSID and TelephoneNumber is always present
In some case <WhoCalledOn> is not present so in the ouput should be reported NULL. In other cases TelephoneNumber rows is reported twice, so output I need is:
Code:
COSID='88',true,Value='393290439266',null
COSID='88',null,Value='393290439266',Value='393290439266'

So, in the first case the tag <WhoCalledOn> is present and the value is true.
In the second case the tag <WhoCalledOn> is not present so should be reported as null, plus, TelephoneNumber is reported twice. Something like:
Code:
<EntityIdentifiers>                   
<Identifier Type='TelephoneNumber' Value='324234231443'/>
<Identifier Type='TelephoneNumber' Value='324234231443'/>
</EntityIdentifiers>

Hope is clear,
thank you,
Lucas
# 4  
Old 03-31-2016
Try (with an extrapolated sample file)
Code:
awk '
/<Activity.*>/  {WHO  = ",null"
                 TEL2 = ",null"
                 TCNT = 0
                }
/COSID=/        {printf "%s", $2
                }
/<WhoCalledOn>/ {gsub (/ *<[^>]*>/, _)
                 WHO = "," $0
                }
/TelephoneNum/  {sub (/.>$/, _, $3)
                 printf "%s,%s", WHO, $3
                 WHO = ""
                 if (TCNT++) TEL2 = ""
                }
/<\/Activit.*>/ {printf "%s%s", TEL2, RS
                }
' file
COSID='88',true,Value='324234231443',null
COSID='76',null,Value='324234231443',null
COSID='69',true,Value='324234231443',Value='3242XXX31443'


Last edited by RudiC; 03-31-2016 at 08:18 AM..
# 5  
Old 03-31-2016
Than you RudiC, you're the king Smilie
I have only to fix something on output since I have:

Code:
,null,,true,
,null,,true,
,null,,true,
COSID='21',null,Value='4354353455',,Value='4354353455'
COSID='21',null,Value='6786786787',,Value='6786786787'
COSID='21',null,Value='345435345345',,Value=345435345345'

When I have two TelephoneNumber, I got double quotes. Most probably because the right structure in the XML file is:


Code:
         <Request>
            <Header>
               <Command>Create</Command>
               <EntityName>Subscriber</EntityName>
               <EntityIdentifiers>
                  <Identifier Type='TelephoneNumber' Value='4354353455'/>
               </EntityIdentifiers>
            </Header>
            <Data>
               <Subscriber>
                  <SubscriberCosName>21</SubscriberCosName>
                  <GlobalLanguageId>it-IT</GlobalLanguageId>
                  <TuiPassword>****</TuiPassword>
                  <GenCustom6>15</GenCustom6>
                  <TelephoneNumber>4354353455</TelephoneNumber>
               </Subscriber>
            </Data>
         </Request>
      </Provisioning>
      <Provisioning xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
         <Response>
            <Header>
               <EntityIdentifiers>
                  <Identifier Type='TelephoneNumber' Value='4354353455'/>
               </EntityIdentifiers>
               <EntityName>Subscriber</EntityName>
               <ResponseStatus>Success</ResponseStatus>

Other strange output is:
Code:
,null,,true,
,null,,true,
,null,,true,

And I don't know why I got it.

Anyway, you save my day, thank you! Smilie
# 6  
Old 03-31-2016
Without seeing the input that resulted in the output posted I can't comment.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract values from xml file script

Hi, please help on this. I want extract values of xml file structure and print in determined way. <ProjectName> --> only appears once <StructList> --> is the top node <Struct> node --> could be more than 1 NameID, STX, STY, PRX, PRY --> appears only 1 time within each <Struct> node... (10 Replies)
Discussion started by: Ophiuchus
10 Replies

2. UNIX for Beginners Questions & Answers

How to change values in xml file?

I have xml file like below, i want change the values at default-value place of each argument name using shell script. like where argument name= protocol and default-value=tcp, where argument name =port and default-value= 7223, where argument name = username and default-value=test, example ... (12 Replies)
Discussion started by: s1s2s3s4
12 Replies

3. Shell Programming and Scripting

How to retrieve values from XML file and update them in the same position! PLEASE HELP?

Good Day All Im quiet new to ksh scripting and need a bit of your help. I am attempting to write a script that reads in an XML and extracts certain field values from an XML file. The values are all alphanumeric and consist of two components: e.g "Test 1". I need to to create a script that... (2 Replies)
Discussion started by: JulioAmerica
2 Replies

4. UNIX for Dummies Questions & Answers

Reading Xml file and print the values into the text file in columnwise?

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (4 Replies)
Discussion started by: sravanreddy
4 Replies

5. UNIX for Dummies Questions & Answers

Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (1 Reply)
Discussion started by: sravanreddy
1 Replies

6. Shell Programming and Scripting

Comparing delta values of one xml file in other xml file

Hi All, I have two xml files. One is having below input <NameValuePair> <name>Daemon</name> <value>tcp:7474</value> </NameValuePair> <NameValuePair> <name>Network</name> <value></value> </NameValuePair> ... (2 Replies)
Discussion started by: sharsour
2 Replies

7. Shell Programming and Scripting

Extract values from an XML File

Hi, I need to capture all the attributes with delete next to it. The source XML file is attached. The output should contain something like this below: Attributes = legacyExchangeDN Action = Delete Username = Hero Joker Loginid = joker09 OU =... (4 Replies)
Discussion started by: prvnrk
4 Replies

8. UNIX for Dummies Questions & Answers

Extracting values from an XML file

Hello People, I have an xml file from which I need to extract the values of the parameters using UNIX shell commands. Ex : Input is like : <Name>Roger</Name> or <Address>MI</Address> I need the output as just : Roger or MI with the tags removed. Please help. (1 Reply)
Discussion started by: sushant172
1 Replies

9. Programming

Extracting Field values for XML file

i have an input file of XML type with data like <nx-charging:additional-parameter name="NX_INTERNATIONALIZED_CLID" value="919427960829"/><nx-charging:finalStatus>RESPONSE , Not/Applicable , OK</nx-charging:finalStatus></nx-charging:process> i want to extract data such that i get the output... (3 Replies)
Discussion started by: junaid.nehvi
3 Replies
Login or Register to Ask a Question