Parsing XML dynamic data via awk?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Parsing XML dynamic data via awk?
# 1  
Old 11-01-2001
Question Parsing XML dynamic data via awk?

I am trying to use a line of output in an XML file as input in another new XML file for processing purposes via a shell script. Since I am a newbie though, I'm not sure how to do this since the data is different everytime. I am using this technique with static data right now:

echo -n "Running data test... "
cat datafile.xml | sed -e s/changethis/intothis/ > /tmp/$$.xml
export RESULT=`~/xml /tmp/$$.xml | grep "<Code>SUCCESS</Code>"`
if [ "$RESULT" != "" ]; then { echo passed } else { echo FAILED ; exit } fi

The XML file has the data I need stored between XML tags, so I know I could grep that line and output it to another file, and use that file to refer to for the input in the next test. That file would have < Number >here'sthedata< /Number > in that file. The trick would be to parse the data out, in effect removing the tags, storing that value in a variable, and then using sed to do a replace in the new input file like I am above. I'm just unsure how to go about parsing the data out.

Hope this wasn't confusing Smilie
# 2  
Old 11-01-2001
Well... if you don't mind going to Perl, (I would recommend this)
you can use the XML::Parser Perl module...

http://wwwx.netheaven.com/~coopercc/...ser/intro.html

...it would be alot more flexable and in the long run,
I think you'll need that anyway. Smilie
# 3  
Old 11-01-2001
Smilie Actually a colleague here is trying to push these scripts over to Perl (mainly because he more comfortable with Perl than shell scripting). To be bluntly honest, I'm a Windows guy (ducks....) and my new job responsibilites are forcing me to pick this up on the fly. From the little exposure I've had to Perl, it probably would work more elegantly, but these testing scripts are all compelte except the one I'm having a problem on, and the ole 'time constraints' make it problematic to rewrite everything in Perl right now. I do plan on doing that eventually, but for now I need this up and running in the shell environment.

(bash bash bash) Smilie
# 4  
Old 11-01-2001
PERL is really not so difficult to learn and there must be hundreds of samples on the net for how to build PERL::XML transformations:


PERL XML FAQ

XMLperl

And so much more:

Google search
# 5  
Old 11-02-2001
If you REALLY, REALLY want to use awk, I guess you can
do somthing like...

Code:
#!/bin/sh
awk '/<Number>/ {stp=index($0,">")+1; \
newstr=substr($0,stp); \
etp=index(newstr,"<")-1; \
print substr(newstr,0,etp); \
}' test.xml

...the test.xml data file is...

Code:
<String>one</String>
<Number>two</Number>
<String>three</String>
<Number>four</Number>

... the awk program will output the "data" on the
"<Number>" lines only.

I hope this encourages you to look at Perl again. Smilie
# 6  
Old 11-02-2001
Damn...

The XML strings disappeard in the previous post...
Note I changed all the angle brackets "<" to square
brackets "[". you'll have to translate them back.

script...

#!/bin/sh

awk '/[Number]/ {stp=index($0,">")+1; \
newstr=substr($0,stp); \
etp=index(newstr,"<")-1; \
print substr(newstr,0,etp); \
}' test.xml


test.xml

[String]one[/String]
[Number]two[/Number]
[String]three[/String]
[Number]four[/Number]


Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing custom data into xml in Shell

Hi , I have data as below in a text file { 'AAA' => { 'A1' => 'a1 comment', 'A2' => 'a2 comment' }, 'BBB' => { 'B1' => 'b1 comment' }, 'CCC' => { 'C1' => 'c1 comment', 'C2' => 'c2 comment', 'C3' => 'c3 comment' 'C4' => 'c4... (2 Replies)
Discussion started by: vivek d r
2 Replies

2. Shell Programming and Scripting

Parsing XML (and insert data) then output data (bash / Solaris)

Hi folks I have a script I wrote that basically parses a bunch of config and xml files works out were to add in the new content then spits out the data into a new file. It all works - apart from the xml and config file format in the new file with XML files the original XML (that ends up in... (2 Replies)
Discussion started by: dfinch
2 Replies

3. Shell Programming and Scripting

XML Parsing using awk

Hi All, I have a problem to resolve. For following XML file, I need to parse the values based on Tag Name. I would prefer to use this by awk. I have used sed command to replace the tags (s/<SeqNo>//). In this case there can be new tags introduced. So need to parse it based on Tag Name. Any... (9 Replies)
Discussion started by: Tons
9 Replies

4. Shell Programming and Scripting

Help - Parsing data in XML in Linux

Hi, I have an XML file in Linux and it contains a long string of characters. The last part of the file is like ....... ....... ....... CAD</MarketDescription></InvestorTransaction></AdvisorAccount></DivisionAdvisor></Division>... (3 Replies)
Discussion started by: naveed
3 Replies

5. Shell Programming and Scripting

xml parsing with awk

hi all.. need your help again.. i have xml file and i want to parsing some data from the xml file.. <ex-name="keroco"> <................> <................> <................> <br-name="cincai"> <ship="123456"> <...................> ... (3 Replies)
Discussion started by: buncit8
3 Replies

6. Shell Programming and Scripting

parsing data from xml file is failing can't open variable

Created a korn shell script, everything is is working except this section, the variable $SYSINFO is being set, but the NASIP & NASDEV are failing, it appears to be treating the config.xml file config directory and xml as the file. Need a second set of eyes to tell me where I am messing up. #... (3 Replies)
Discussion started by: juanb25
3 Replies

7. Shell Programming and Scripting

Parsing dynamic data from a command output

Hi people, I am writing a korn shell script, and one of the command gives an output something like below: release.label.2010.03.02 objects: /project/path/to/some/file_name.ksh /project/path/another/file_name01.dat I have to retrieve the file paths one by one & use them as... (9 Replies)
Discussion started by: kiwin1000
9 Replies

8. Shell Programming and Scripting

Parsing posted XML data from a remote server?

Hi Is it possible to parse a posted xml data from a remote server in unix shell script. if so how to do that? and i need to give this script path in the push url (in remote server) . how to do this? I have tried this in asp but could not succeed....so am trying in shell scripting...the thread... (1 Reply)
Discussion started by: aemunathan
1 Replies

9. Shell Programming and Scripting

parsing xml using awk

hello , i am trying to parse xml using awk however its a little bit tricky as i want <databases> <source> <host>prod</host> <port>1522</port> <tns>GP1</tns> <user>P11</user>... (6 Replies)
Discussion started by: amit1_x
6 Replies

10. Shell Programming and Scripting

Parsing and getting data from XML file using ksh script

Hi All, I have a xml file for example as described below <xml> <address> <street><street> <address/> <isbn>426728783932020308393930303</isbn> <book> <name> </name> </book> . . . </xml> My problem is to get the isbn number from the above described file using ksh script. Could... (6 Replies)
Discussion started by: vinna
6 Replies
Login or Register to Ask a Question