Help with XML tag value extraction based on condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with XML tag value extraction based on condition
# 1  
Old 07-17-2017
Help with XML tag value extraction based on condition

sample xml file part

Code:
<?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>
  <ActualTemplateObject _LoadId="export_AJ6iAFmhclFKgb" _Logical="true" class="ariba.collaborate.contracts.ContractWorkspace" ref="true">
    <Workspace>/Templates/Contract Templates/Contract Workspace Professional/OPE Workflow#3/</Workspace>
  </ActualTemplateObject>
  <AgreementPath>/tsoqrqut.4w25m5i</AgreementPath>
  <Alert>Gray</Alert>
  <AllowAdhocSpend>false</AllowAdhocSpend>
  <Amount>$15,000.00 USD</Amount>

Hi ,
I want to search for "ContractWorkspace.xsd" in the in the first line of the xml file and print the value "CW2218471" from the tag <_LocalId> along with value "True" separated by comma delimiter. If ContractWorkspace.xsd does not exist in the first line of the xml file , I want to still print whatever value exists in corresponding <_LocalId> tag along with value "False" separated by comma delimiter. Can you please help me?

for eg: sample output expected if a match is found for string "ContractWorkspace.xsd" in the first line of xml file
=> CW2218471,True

sample output expected if a match is not found for string "ContractWorkspace.xsd" in the first line of xml file
=> value from <_LocalId> tag ,False



Moderator's Comments:
Mod Comment Please use CODE tags correctly as required by forum rules!
DON'T just enclose the entire post in code tags!

Last edited by RudiC; 07-17-2017 at 07:42 PM.. Reason: Changed CODE tags.
# 2  
Old 07-17-2017
Hello paul1234,

Could you please try following and let me know if this helps you.
Code:
awk -F"[><]" '/ContractWorkspace.xsd/{A=1;next} A && /<_LocalId>/{val=$3;next} /<Active>/ && A{print val",True";A="";next} /<Active>/ && !A{print val",False"}'   Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 07-18-2017 at 02:45 AM..
# 3  
Old 07-17-2017
Try:
Code:
awk '/ContractWorkspace\.xsd/{f=1} $1=="_LocalId"{print $2 (f?",True":",False")}' RS=\< FS=\> file

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 07-18-2017
Thank you Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving XML tag/contents after specific XML tag within same file

Hi Forum. I have an XML file with the following requirement to move the <AdditionalAccountHolders> tag and its content right after the <accountHolderName> tag within the same file but I'm not sure how to accomplish this through a Unix script. Any feedback will be greatly appreciated. ... (19 Replies)
Discussion started by: pchang
19 Replies

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

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

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

Multi line extraction based on condition

Hi I have some data in a file as below ****************************** Class 1A Students absent are : 1. ABC 2. CDE 3. CPE ****************************** Class 2A Students absent are : ****************************** Class 3A Students absent are : (6 Replies)
Discussion started by: reldb
6 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. Shell Programming and Scripting

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/"... (3 Replies)
Discussion started by: Neethu
3 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