Extract values from xml file script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract values from xml file script
# 8  
Old 07-02-2018
Quote:
Originally Posted by Don Cragun
No. You have not answered our doubts. You gave us sample input in post #1 and you showed us the output you wanted from that input. And, you were given code that produced that output from that input and then you changed your requirements.

In post 4 you gave us new sample input and you said "The output for this input file 2 would be like this:" and you showed us some output. But with that wording, I don't know if you are saying that that is the output you get from some code that has been suggested (but not what you want), that it is the output you get from some other code that you're using (but not what you want), or if it is the output you want from that new input.

Furthermore, you haven't clearly specified whether the original input you provided in post #1 was valid input that did not include data that was needed to trigger special cases that were missing from your original algorithm (and the code you want should still provide the output you said you want from that input in post #1) or if the input you provided in post #1 was not valid input and everything you said about the output you wanted to be produced from that input should be ignored.
Hi Don,

1) The output I show for sample 2 is my desired output for sample file 2, written/edited manually by me not by any other code(I donīt have more codes Smilie ).
2) Yes, input file in post #1 was valid input that did not include data that was needed to trigger special cases. I noticed that was needed to add data to sample file to consider special cases when I tested the Chubler_XLīs code in sample #2.
3) The code provided by Chubler_XL it works for the sample of post #1. The structure of output I was looking for help in post #1 is the same structure of output for sample #2,
but sample #2 includes some other nodes (<XB_Ranges> and <KJ_Ranges>) that have the same name for their sub-children. That makes the script confuses the values in the output. When I tested the Chubler_XL solution in sample #2 I realized the output was not the desired one. In this point I saw was needed to add those other nodes in order to be considered in script.
4) In addition, in real file the nodes <STR_VAL> and <PRO_VAL> go after <PR_Ranges>. Since the real xml is a kind of large with a lot of other nodes, I only focused in the nodes I want to
extract and the sample I created for post #1 was not accurate in the order of nodes.

I hope be more clear.

Thanks for the help and sorry for any inconvenience.
# 9  
Old 07-02-2018
How about this:

Code:
awk -F"<|>" '
BEGIN{print "ProjectName|NameID|STX-STY|PRX-PRY|OD-ODF|ODRangeStart|ODRangeStop"}
/ProjectName/{printf "%s",$3}
/<.StructData>/{
    for(i=1; i<=rngln; i++) {
       printf "|%s|%s|%s|%s|%s|%s\n",\
          id,stx?stx"-"sty:"",\
          prx?prx"-"pry:"",\
          od[i]?od[i]"-"odf[i]:"",\
          rngStart[i],rngStop[i]
       delete od[i]
       delete odf[i]
       delete rngStart[i]
       delete rngStop[i]
       id=stx=sty=prx=pry=""
    }
    rngln=0
}
/NameID/{id=$3}
/<STX>/{stx=$3}
/<STY>/{sty=$3}
/<PRX>/{prx=$3}
/<PRY>/{pry=$3}
/<PR_Ranges>/ {PRActive=1}
!PRActive { next }
/<.PR_Ranges>/ {PRActive=0}
/<OD>/ {od[++rngln]=$3}
/ODF>/{odf[rngln]=$3}
/ODRangeStart/{rngStart[rngln]=$3}
/ODRangeStop/{rngStop[rngln]=$3}
' infile2

This User Gave Thanks to Chubler_XL For This Post:
# 10  
Old 07-02-2018
Quote:
Originally Posted by Chubler_XL
How about this:
How can I say. It works just exactly as I was looking for.

I tested changing the "delete array[i]" sentences to outside for loop as "delete array" and it works too.

Many thanks for your help again.
# 11  
Old 07-02-2018
Quote:
Originally Posted by Ophiuchus
How can I say. It works just exactly as I was looking for.

I tested changing the "delete array[i]" sentences to outside for loop as "delete array" and it works too.

Many thanks for your help again.
The standards specify that every conforming version of awk must support delete array[subscript]. Some implementations of awk also support delete array, but that is not required by the standards. On systems that do not support delete array, you can use split("", array) to get the same effect.

So, if you're trying to write code that will work on every implementation, avoid using delete array. If you're just writing code to be used on your current system,
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to extract xml attribute values using awk inline.?

I am trying to extract specific XML attribute values for search pattern <factories.*baseQueueName' from resources.xml. my scripts works ok,, but to extract 3 values this code does echo $line three times, it could be 'n' times. How can I use awk to extract matching pattern values in-line or... (11 Replies)
Discussion started by: kchinnam
11 Replies

2. UNIX for Dummies Questions & Answers

Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (1 Reply)
Discussion started by: sravanreddy
1 Replies

3. Shell Programming and Scripting

Passing values to an XML file from shell script

:wall: Hi, I have an XML file with 5 tags. I need to pass values to the XML file from a shell script that will replace values in 2 of the tags. I cannot hardcode the tag values in XML and use replace command in script as the values are likely to change. Please help !!!!!!!!!!! (2 Replies)
Discussion started by: Monalisaa
2 Replies

4. Shell Programming and Scripting

Get multiple values from an xml file using one of the following commands or together awk/perl/script

Hello, I have a requirement to extract the value from multiple xml node and print out the values to new file to compare. Would be done using either awk/perl or some unix script. For example sample input file: ..... ..... <factories xmi:type="resources.jdbc:DataSource"... (2 Replies)
Discussion started by: slbmind
2 Replies

5. Shell Programming and Scripting

Extract values from an XML File

Hi, I need to capture all the attributes with delete next to it. The source XML file is attached. The output should contain something like this below: Attributes = legacyExchangeDN Action = Delete Username = Hero Joker Loginid = joker09 OU =... (4 Replies)
Discussion started by: prvnrk
4 Replies

6. Shell Programming and Scripting

Perl script to extract 'ID' From XML File

File1.xml <?xml version.........> - <abcd:abcd_list version="1" www.john_uncle's_server.com" xmlns: - <device id="100"> <firmware>12.4(3d)</firmware> <location id="500">Sitting Room</location> </device> - <device id="101"> <firmware>12.4(3d)</firmware> <location id="501">Class... (1 Reply)
Discussion started by: sureshcisco
1 Replies

7. Shell Programming and Scripting

KSH Script to Get the <TAG Values> from an XML file

Hi All, I am new to Unix I need a KSH script to get the values from XML file to write to a temp file. Like the requirement is from the below TAG <MAPPING DESCRIPTION ="Test Mapping" ISVALID ="YES" NAME ="m_test_xml" OBJECTVERSION ="1" VERSIONNUMBER ="1"> I need the MAPPING DESCRIPTION... (3 Replies)
Discussion started by: perlamohan
3 Replies

8. Shell Programming and Scripting

Extract XML Element Values

I have a rather large file with XML-style content. Each line contains one full XML entry. For example: 1:<Message><DNIS>1234</DNIS><UCID>3456</UCID><TransferGroup>XYZXYZ</TransferGroup></Message> 2:<Message><DNIS>9999</DNIS><UCID>2584</UCID><TransferGroup>ABCABC</TransferGroup></Message>... (1 Reply)
Discussion started by: sharpi03
1 Replies

9. Shell Programming and Scripting

Help with shell script to extract data from XML file

Hello Scripting Gurus, I need help with extracting data from the XML file using shell script. The data is in a large XML and I need to extract the id values of all completedworkflows. Here is a sample of it. Input and output data is also in the attached text files. <wfregistry>... (5 Replies)
Discussion started by: yajaykumar
5 Replies
Login or Register to Ask a Question