how to find the end tag in xml file.

Hi,

I am newbie. I wanted to know how to find the end tag in shell where I have multiple tags in xml like

<tag name="x" version="1.0">
</tag>
<tag name="x" version="1.0">
</tag>
<tag name="x" version="1.0">
</tag>

And I wanted to update depends this xml as (in side the tag <subtag name="y"/>) depending upon the conditions like if 1,I have to update the first tag with the above mentioned tag and second and so on..

Thanks,
Maria Kumar

Please mention your desired output.

I have a string like x,y,z and xml file contains
<tag name="x" version="1.0">
</tag>
<tag name="y" version="1.0">
</tag>
<tag name="z" version="1.0">
</tag>

If I pass x then 'x' application getting updated as
<tag name="x" version="1.0">
<sometext/>
</tag>
If I pass y then 'y' application getting updated as
<tag name="y" version="1.0">
<sometext/>
</tag>

If I pass z then 'z' application getting updated as
<tag name="z" version="1.0">
<sometext/>
</tag>

---------- Post updated at 12:34 PM ---------- Previous update was at 12:32 PM ----------

For that I am searching every end tag then navigate to start tag then I will use sed to update the particular tag.

Write one script:

#!/bin/ksh

touch optFile         # create empty output file
echo "Enter tag name:\c"       # ask input tag name. E.g. "x" or "y"
read tag                          # read input

cat xmlFile | while read line     # read each line of xmlfile
do
   if [ "$line" = ".*\<tag name\=\"$tag\".*" ] ; then         # if line contains matching tag then
       echo "$line" >> optFile                                      # write original line in output file
       echo "\<sometext\/\>" >> optFile                          # append extra line after matching tag line in output file
   else                                                                      # else
       echo "$line" >> optFile                                     # write line as it is in output file
   fi
done

Can you explain Plz..

I have edited the solution. Please Contact for more help.

Thanks for your solution. I will verify it.

---------- Post updated at 01:16 PM ---------- Previous update was at 01:04 PM ----------

cat xmlFile | while read line # read each line of xmlfile
do
if [ "$line" = ".\<tag name\=\"$tag\"." ] ; then # if line contains matching tag then
echo "$line" >> optFile # I need to get </tag> so that I can use this command==> sed '/<\/tag>/i\ <sometext\/\> ' $file $file2
echo "\<sometext\/\>" >> optFile # append extra line after matching tag line in output file
else # else
echo "$line" >> optFile # write line as it is in output file
fi
done

The desirable output can be achieved by the given solution. Why do you want to use sed command specifically. Is there any particular reason behind it?

When I print $line it showing as x/y/z it not showing </tag>. We have two files ,need to append this details from one file to another.

Show whatever output you got.

It is not appending any thing to the file.
Here is my code:

touch optFile

a1="X"

echo $a1

cat $A2 | while read line
do
if [ "$line" = ".\<tag name\=\"$a1\"." ] ; then

    echo "$line" &gt;&gt; optFile               
   echo "\\&lt;sometext\\/\\&gt;" &gt;&gt; optFile  

else
echo "$line" >> optFile
fi
done

And O/P file as below: It was same as I/P file
<tag name="X">

</tag>
<tag name="Y">

</tag>