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 matching condition

Shell Programming and Scripting


Tags
solved, solved awk

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago
paul1234 paul1234 is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 26 July 2017, 11:20 AM EDT
Posts: 16
Thanks: 3
Thanked 0 Times in 0 Posts
Help with XML tag value extraction based on matching condition

sample xml file part

Code:
<DocumentMinorVersion>0</DocumentMinorVersion>
  <DocumentVersion>1</DocumentVersion>
  <EffectiveDate>2017-05-30T00:00:00Z</EffectiveDate>
  <FollowOnFrom>
    <ContractRequest _LoadId="export_AJ6iAFoh6g0rE9">
      <_LocalId>CRW2218451</_LocalId>
      <Active>true</Active>
      <ActualTemplateObject _LoadId="export_AJ6iAFoag0rxLm" _Logical="true" class="ariba.collaborate.contracts.ContractRequest" ref="true">
        <Workspace>/Templates/Contract Templates/Contract Request/</Workspace>

I want to get the value inside the tag <_LocalId> and </_LocalId> ,only if the <workspace> tag has a value matching to string "Contract Request"
Remember the above snippet is only a portion of the big file and the same tags may be repeating with other values in the file. But I do not want to search till the end of the file too .
I want to stop the search at the very first match in the file and get that one value out

expected output

Code:
CRW221845


Moderator's Comments:
Help with XML tag value extraction based on matching condition Please DON'T use CODE tags for the entire text, but for code and data only!

Last edited by RudiC; 1 Week Ago at 05:23 PM.. Reason: Moved code tags.
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 27 July 2017, 2:54 PM EDT
Location: Chennai
Posts: 2,563
Thanks: 564
Thanked 1,209 Times in 1,088 Posts
Hello paul1234,

Could you please try following and let me know if this helps you.
Solution 1st: Looking for string <_LocalId> with making field separator -F[[><]' as follows.

Code:
awk -F'[><]' '/<_LocalId>/{print $3}'   Input_file

Solution 2nd: Looking for string <_LocalId> and then substituting everything till > and then globally substituting > and <.* too and then printing the line.

Code:
awk '/<_LocalId>/{sub(/.[^>]*/,"");gsub(/>|<.*/,"");print}'   Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 1 Week Ago at 02:01 PM..
The Following User Says Thank You to RavinderSingh13 For This Useful Post:
paul1234 (1 Week Ago)
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago
paul1234 paul1234 is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 26 July 2017, 11:20 AM EDT
Posts: 16
Thanks: 3
Thanked 0 Times in 0 Posts
Hi Ravinder ,
I had tried this already .The LocalId tag repeats in the file so I wanted to get the value of LocalId tag only if the Workspace value has the string "Contract Request".
    #4  
Old Unix and Linux 1 Week Ago
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 27 July 2017, 2:54 PM EDT
Location: Chennai
Posts: 2,563
Thanks: 564
Thanked 1,209 Times in 1,088 Posts
Quote:
Originally Posted by paul1234 View Post
Hi Ravinder ,
I had tried this already .The LocalId tag repeats in the file so I wanted to get the value of LocalId tag only if the Workspace value has the string "Contract Request".
Hello paul1234,

Apologies missed it, please write only code in the code tags and information out side of code tags like I am writing here as an example. Could you please try following and let me know if this helps.
Solution 1st:

Code:
awk '/<_LocalId>/{sub(/.[^>]*/,"");gsub(/>|<.*/,"");val=$0;next} /<Workspace>.*Contract Request/{print val;exit}'  Input_file

Solution 2nd:

Code:
awk -F'[><]' '/<_LocalId>/{val=$3;next} /<Workspace>.*Contract Request/{print val;exit}'   Input_file

Thanks,
R. Singh
The Following User Says Thank You to RavinderSingh13 For This Useful Post:
paul1234 (1 Week Ago)
Sponsored Links
    #5  
Old Unix and Linux 1 Week Ago
paul1234 paul1234 is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 26 July 2017, 11:20 AM EDT
Posts: 16
Thanks: 3
Thanked 0 Times in 0 Posts
Hi Ravinder ..This was very useful . Thank you for your helpLinux
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
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
Extraction of strings from a file, after pattern matching abkush UNIX for Dummies Questions & Answers 3 10-21-2011 08:08 AM
Report file extraction based on Date range ganapati Shell Programming and Scripting 2 07-13-2006 11:26 AM
Pattern-Matching in If-Condition jzillan Shell Programming and Scripting 6 12-20-2005 05:03 PM



All times are GMT -4. The time now is 02:55 PM.