Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Help with XML tag value extraction based on condition

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-17-2017
paul1234 paul1234 is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 13 September 2017, 4:10 PM EDT
Posts: 23
Thanks: 6
Thanked 0 Times in 0 Posts
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:
Help with XML tag value extraction based on condition 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 06:42 PM.. Reason: Changed CODE tags.
Sponsored Links
    #2  
Old Unix and Linux 07-17-2017
RavinderSingh13 RavinderSingh13 is offline Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 15 September 2017, 12:22 AM EDT
Location: Chennai
Posts: 2,606
Thanks: 571
Thanked 1,238 Times in 1,116 Posts
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 01:45 AM..
Sponsored Links
    #3  
Old Unix and Linux 07-17-2017
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 20 September 2017, 2:01 PM EDT
Location: Amsterdam
Posts: 11,533
Thanks: 500
Thanked 3,335 Times in 2,943 Posts
Try:

Code:
awk '/ContractWorkspace\.xsd/{f=1} $1=="_LocalId"{print $2 (f?",True":",False")}' RS=\< FS=\> file

The Following User Says Thank You to Scrutinizer For This Useful Post:
paul1234 (07-18-2017)
    #4  
Old Unix and Linux 07-18-2017
paul1234 paul1234 is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 13 September 2017, 4:10 PM EDT
Posts: 23
Thanks: 6
Thanked 0 Times in 0 Posts
Thank you Linux
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with XML tag value extraction based on matching condition paul1234 Shell Programming and Scripting 4 07-14-2017 04:56 PM
Copy down based on condition techedipro Shell Programming and Scripting 1 03-27-2015 01:55 AM
Multi line extraction based on condition reldb Shell Programming and Scripting 6 07-26-2014 11:17 AM
Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds) asjaiswal UNIX for Dummies Questions & Answers 9 11-28-2013 05:11 PM
Report file extraction based on Date range ganapati Shell Programming and Scripting 2 07-13-2006 11:26 AM



All times are GMT -4. The time now is 03:52 PM.