Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2006
    Posts
    2

    Unanswered: 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.

  2. #2
    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 07:10.

  3. #3
    Join Date
    Aug 2006
    Posts
    2
    hmmm, interesting thanks i will try that. Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •