Help with XML tag value extraction based on matching condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with XML tag value extraction based on matching condition
# 1  
Old 07-14-2017
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:
Mod Comment Please DON'T use CODE tags for the entire text, but for code and data only!

Last edited by RudiC; 07-14-2017 at 06:23 PM.. Reason: Moved code tags.
# 2  
Old 07-14-2017
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; 07-14-2017 at 03:01 PM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 07-14-2017
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 07-14-2017
Quote:
Originally Posted by paul1234
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 07-14-2017
Hi Ravinder ..This was very useful . Thank you for your helpSmilie
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 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

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

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

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

10. Shell Programming and Scripting

perl regexp: matching the parent xml tag

Hi folks. I would like to remove the full parent (outer) xml tag from a file given a matching child (inner) tag, in a bash shell. To be more specific, this is what I have so far: $ cat myFile.xml <Sometag></Sometag> <Outer> <Inner>1</Inner> </Outer> <Outer> <stuff>alot</stuff> ... (3 Replies)
Discussion started by: BatManWSL
3 Replies
Login or Register to Ask a Question