how to read the variable from tags based on appropriate tag


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to read the variable from tags based on appropriate tag
# 1  
Old 10-04-2012
how to read the variable from tags based on appropriate tag

Hi,

I've got a situation where I need to read the values from XML tags in a file. Please find the sample xml code below:

Code:
<entity>
  <name>Testing</name>
  <number>11</number>
  <template>testing.testing</template>
</entity>
<entity>
  <name>Development</name>
  <number>13</number>
</entity>
<entity>
  <name>Support</name>
  <number>18</number>
  <location>US</location>
  <template>analyze.analyze</template>
</entity>

Now, i want to retrieve the <name> tag field and <template> tag value using single command. But, the <template> tag doesnot exists for some fields.

output should be:
Code:
NAME - TEMPLATE
Testing - testing
Development - 
Support - analyze

In the output, TEMPLATE column should have only first string before . (period) sign.

Please help me out!!!

Thanks in advance

Last edited by Scrutinizer; 10-04-2012 at 06:46 AM.. Reason: code tags instead of quote tags
# 2  
Old 10-04-2012
This works on your sample data:
Code:
awk -F'[<>.]' '/<entity>/,/<\/entity>/{
if(/<name>/) n=$3
if(/<template>/) t=$3
if(/<\/entity>/) {print n,t;n=t=""}}' OFS=' - ' file

This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 10-04-2012
Code:
awk -F "[<>.]" 'NR==1{print "Name Template"}
/<name>/{if(s){print s"-";s=$3}else{s=$3}}
/<template>/{s=s"-"$3;print s;s=""}' file

This User Gave Thanks to pamu For This Post:
# 4  
Old 10-04-2012
Quote:
Originally Posted by elixir_sinari
This works on your sample data:
Code:
awk -F'[<>.]' '/<entity>/,/<\/entity>/{
if(/<name>/) n=$3
if(/<template>/) t=$3
if(/<\/entity>/) {print n,t;n=t=""}}' OFS=' - ' file


Hi Thanks for the update.

A small update that if I don't want to read the values of <name> of <entity> in which <template> doesn't present. Could you help??

---------- Post updated at 03:26 PM ---------- Previous update was at 03:23 PM ----------

Quote:
Originally Posted by pamu
Code:
awk -F "[<>.]" 'NR==1{print "Name Template"}
/<name>/{if(s){print s"-";s=$3}else{s=$3}}
/<template>/{s=s"-"$3;print s;s=""}' file

Hi Thanks you.

I don't want to read the values of <name> of <entity> in which <template> doesn't present. Could you help??
# 5  
Old 10-04-2012
You could have mentioned that in the original post itself. Your output in that post did not require this constraint. In future, mention your requirements clearly and fully.
Code:
awk -F'[<>.]' '/<entity>/,/<\/entity>/{
if(/<name>/) n=$3
if(/<template>/) t=$3
if(/<\/entity>/) {if(t) print n,t;n=t=""}}' OFS=' - ' file

This User Gave Thanks to elixir_sinari For This Post:
# 6  
Old 10-04-2012
Code:
awk -F "[<>.]" 'NR==1{print "Name Template"}
/<name>/{s=$3}
/<template>/{s=s"-"$3;print s;s=""}' file

This User Gave Thanks to pamu For This Post:
# 7  
Old 10-04-2012
Quote:
Originally Posted by pamu
Code:
awk -F "[<>.]" 'NR==1{print "Name Template"}
/<name>/{s=$3}
/<template>/{s=s"-"$3;print s;s=""}' file

Hi

It works fine. Thank you. Apologize to bother you again.

I've got an another scenario to read values. Please find the below sample xml code:

Code:
<entity>
 <name>Testing</name>
 <number>13</number>
 <template>analyze.analyze</template>
 <requirement>
    <prerequire>
       <prerequireId>12</prerequireId>
       <prerequire>439</prerequire>
   </prerequire>
  </requirement>
</entity>
<entity>
 <name>Support</name>
 <number>19</number>
 <template>testing.testing</template>
 <requirement>
    <prerequire>
       <prerequireId>10</prerequireId>
       <prerequire>9382</prerequire>
   </prerequire>
    <prerequire>
       <prerequireId>10</prerequireId>
       <prerequire>9382</prerequire>
   </prerequire>
  </requirement>
</entity>
<entity>
 <name>Development</name>
 <number>14</number>
 <template>testing.testing</template>
 <requirement>
    <prerequire>
       <prerequireId>11</prerequireId>
       <prerequire>1928</prerequire>
   </prerequire>
    <prerequire>
       <prerequireId>12</prerequireId>
       <prerequire>1458</prerequire>
   </prerequire>
    <prerequire>
       <prerequireId>9</prerequireId>
       <prerequire>9894</prerequire>
   </prerequire>
  </requirement>
</entity>

The output should display as below:
Quote:
Name Template Prequire1 Prequire2 Prequire3
Testing analyze 439
Support testing 9382 9382
Development testing 1928 1458 9894
In the above sample xml code, the number of <prequire> tag may vary or may not exists.
Please help!!!!

---------- Post updated at 05:12 PM ---------- Previous update was at 05:11 PM ----------

Quote:
Originally Posted by elixir_sinari
You could have mentioned that in the original post itself. Your output in that post did not require this constraint. In future, mention your requirements clearly and fully.
Code:
awk -F'[<>.]' '/<entity>/,/<\/entity>/{
if(/<name>/) n=$3
if(/<template>/) t=$3
if(/<\/entity>/) {if(t) print n,t;n=t=""}}' OFS=' - ' file

Apologize to bother you again Smilie

I've got an another scenario to read values. Please find the below sample xml code:

Code:
<entity>
 <name>Testing</name>
 <number>13</number>
 <template>analyze.analyze</template>
 <requirement>
    <prerequire>
       <prerequireId>12</prerequireId>
       <prerequire>439</prerequire>
   </prerequire>
  </requirement>
</entity>
<entity>
 <name>Support</name>
 <number>19</number>
 <template>testing.testing</template>
 <requirement>
    <prerequire>
       <prerequireId>10</prerequireId>
       <prerequire>9382</prerequire>
   </prerequire>
    <prerequire>
       <prerequireId>10</prerequireId>
       <prerequire>9382</prerequire>
   </prerequire>
  </requirement>
</entity>
<entity>
 <name>Development</name>
 <number>14</number>
 <template>testing.testing</template>
 <requirement>
    <prerequire>
       <prerequireId>11</prerequireId>
       <prerequire>1928</prerequire>
   </prerequire>
    <prerequire>
       <prerequireId>12</prerequireId>
       <prerequire>1458</prerequire>
   </prerequire>
    <prerequire>
       <prerequireId>9</prerequireId>
       <prerequire>9894</prerequire>
   </prerequire>
  </requirement>
</entity>

The output should display as below:
Quote:
Name Template Prequire1 Prequire2 Prequire3
Testing analyze 439
Support testing 9382 9382
Development testing 1928 1458 9894
In the above sample xml code, the number of <prequire> tag may vary or may not exists.
Please help!!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replacing tag based on condition

Hi All, I am having a file like below. The file will having information about the records.If you see the file the file is header and data. For example it have 1 men tag and the tag id will be come after headers. The change is I want to convert All pets tag from P to X. I did a sed like below... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

2. 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

3. 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

4. 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

5. Shell Programming and Scripting

Read xml tags and then remove the tag using shell script

<Start> <Header> This is header section </Header> <Body> <Body_start> This is body section <a> <b> <c> <st>111</st> </c> <d> <st>blank</st> </d> </b> </a> </Body_start> <Body_section> This is body section (3 Replies)
Discussion started by: RJG
3 Replies

6. Shell Programming and Scripting

Convert tag based lines to xml format

Hi All, Can some one help me to convert this line of code to xml format. Thanks in advance, preethy. input: ... (2 Replies)
Discussion started by: preethy
2 Replies

7. UNIX for Advanced & Expert Users

Shell Script to read XML tags and the data within that tag

Hi unix Gurus, I am really new to Unix Scripting. Please help me to create a shell script which reads the xml file and from that i need to fetch a particular information. For example <SOURCE BUSINESSNAME ="" DATABASETYPE ="Teradata" DBDNAME ="DWPROD3" DESCRIPTION ="" NAME... (2 Replies)
Discussion started by: SmilePlease
2 Replies

8. 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

9. Shell Programming and Scripting

mp3 tag/rename based on creation (last modified date)

Arg, I'm trying to figure out how to create a album tag based on the last modified date stamp for files which don't have a corresponding .talk file. IE. 2009 12 10 - Talk Radio.mp3 is how I want them structured, they should all have a corresponding .talk file so my mp3 player can speak the name ie... (0 Replies)
Discussion started by: mrplow
0 Replies

10. Shell Programming and Scripting

read xml tag attribute and store it in variable

Hi, How to read xml tag attributes and store into variable in shell script? Thanks, Swetha (5 Replies)
Discussion started by: swetha123
5 Replies
Login or Register to Ask a Question