Creating multiple xml tag lines in a file

Hi All,

Can someone tell me how can we create same xml tag lines based on the number of lines present in other file and replace the Name variable vaule present in other file.

basically I have this xml line

 <typ:RequestKey NameType="RIC" Name="A1" Service="DDA"/>
 

and say I have Namesfile which has 500 names/lines in it, all I need to do is read this file and create the above said xml tag by replacing the value of the Name=

ex : Namefile contains 5 lines

 A1
 A2
 A3
 A4
 A5
 

now, once script is run, I should the 5 xml tag lines as output ie,

 <typ:RequestKey NameType="RIC" Name="A1" Service="DDA"/>
 <typ:RequestKey NameType="RIC" Name="A2" Service="DDA"/>
 <typ:RequestKey NameType="RIC" Name="A3" Service="DDA"/>
 <typ:RequestKey NameType="RIC" Name="A4" Service="DDA"/>
 <typ:RequestKey NameType="RIC" Name="A5" Service="DDA"/>
 

What have you tried so far?

1 Like

Thanks Don.

I tried it by myself, it simple while loop did the trick..

Many Thanks..

 #!/usr/bin/bash
 while read line
do
        #echo "Generating xml tag is :<typ:RequestKey NameType="RIC" Name="A1" Service="DDA"/>"
        echo "<typ:RequestKey NameType="RIC" Name="\"$line\"" Service="DDA"/>" >> /tmp/xmlfile
done < /tmp/xmllist

In awk you could try like this

$ awk '{print "<typ:RequestKey NameType=" q "RIC" q  " Name=" q $0 q " Service=" q "DDA" q "/>"}' q='"' file

Test :

$ echo 1111 | awk '{print "<typ:RequestKey NameType=" q "RIC" q  " Name=" q $0 q " Service=" q "DDA" q "/>"}' q='"'
<typ:RequestKey NameType="RIC" Name="1111" Service="DDA"/>

Thanks for sharing, Optimus. Good that you found a solution.

Some remarks:
The shebang ( #! ) should be in the first two positions of the script, there should not be a space in front of it, nor an empty line above it..

The quoting in the echo statement is not right (for example the variable $line is unquoted), it should be:

echo "<typ:RequestKey NameType=\"RIC\" Name=\"$line\" Service=\"DDA\"/>"

or for example:

printf '<typ:RequestKey NameType="RIC" Name="%s" Service="DDA"/>\n' "$line"

or

cat << EOF
<typ:RequestKey NameType="RIC" Name="$line" Service="DDA"/>
EOF

The second EOF should be at the first position of the line.

instead of redirecting the echo statement and repeatedly appending to a file, you could also redirect the output of the loop. So it becomes:

#!/usr/bin/bash
while read line
do
  # echo "Generating xml tag is :<typ:RequestKey NameType="RIC" Name="A1" Service="DDA"/>"
  echo "<typ:RequestKey NameType=\"RIC\" Name=\"$line\" Service=\"DDA\"/>"
done < /tmp/xmllist > /tmp/xmlfile