If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Locating correct XML tags

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-14-06, 20:48
ahtat99 ahtat99 is offline
Registered User
 
Join Date: Aug 2006
Posts: 2
Locating correct XML tags

hi all, I have a question for you all. I have a xml file that contains 2 similar tags, like the example below,
<some tag>
<Tag ABC=1>
<tag_value1>some_value</tag_value1>
</Tag>
</some tag>
...
<Tag ABC=1>
<tag_value1>some_value</tag_value1>
</Tag>

My question would be, how do you retrieve the second <Tag ABC=1>, and then update the some_value right between the <tag_value1>?

I had created following scripts (a section of the whole scripts)
--------------------------------------------------------------------------
cat (file.xml) | while read data
newValue="<tag_value1>some_other_value</tag_value1>"
xmlHelper="echo $data"
echo "$xmlHelper" > $tempXML
strHelper="<Tag ABC=1>\n <tag_value1>some_value</tag_value1>"
oldValue=$(awk -F 'BEGIN{FS="<tag_value1>|</tag_value1>"}
/myMatch/{
for(i=1;i<=NF;i++) {
if [[ ${xmlHelper} = ${strHelper} ]]
then
print "<tag_value1>\(.*)</tag_value1>"
}
}
END {print $2}' file.xml)

if [[ ${xmlHelper} = ${strHelper} ]]; then
sed -e 's/$oldValue/newValue' file.xml
--------------------------------------------------------------------------

I am using ksh to do all these, the problem is that it would not run. Kept giving me an error of awk at line 2. Tried to go around but it come back the same way.
Reply With Quote
  #2 (permalink)  
Old 08-15-06, 05:05
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Hi, there are several things incorrect in your awk program. Here's my interpretation of the syntax which (as I believe) fits your semantics.

Code:
awk 'BEGIN{FS="<tag_value1>|</tag_value1>"}
    /myMatch/{
               for(i=1;i<=NF;i++) {
                   if(var1 = var2)
                       print "<tag_value1>\(.*)</tag_value1>"
               }
           }
    END {print $2}' var1=${xmlHelper} var2=${strHelper} file.xml
First: your -F flag is used to define an alternate fieldseparator and the same you did in the BEGIN section. You could use awk -F'<tag_value1>|<tag_value2>' and skip the BEGIN section.
Second: awk is a small though very own programming language. You can incorporate awk commands inside a shell script but not the other way around. So the syntax of your if-statement should be as described here, whith awk variables holding the expression values. How these variables recieve values form the invoking environment is shown on the last line. After the end bracket you specify the filename(s) to be processed by awk. If a filename has the form var=text it is treated as an assigment of text to var at the time when that argument would otherwise be accessed as a file.

Regards

Last edited by Tyveleyn; 08-15-06 at 06:10.
Reply With Quote
  #3 (permalink)  
Old 08-15-06, 05:10
ahtat99 ahtat99 is offline
Registered User
 
Join Date: Aug 2006
Posts: 2
hmmm, interesting thanks i will try that. Thank you.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On