Create one xml file from one list of names

Actually I have one list of channels names like:

Rai 1
Rai 1 +1HD
Rai 1 +2HD
Rai 2
Rai 2 +1HD
Rai 2 +2HD
.
.
.
.

From this list of names I need create one new xml file with this structure

 <channel id="Rai 1">
    <display-name lang="it">Rai 1</display-name>
    <icon src="http://www.my-website.com/logos/Rai_1.png" />
 </channel>
 <channel id="Rai 1 +1HD">
    <display-name lang="it">Rai 1 +1HD</display-name>
    <icon src="http://www.my-website.com/logos/Rai_1_+1HD.png" />
  </channel>
  <channel id="Rai 1 +2HD">
    <display-name lang="it">Rai 1 +2HD</display-name>
    <icon src="http://www.my-website.com/logos/Rai_1_+2HD.png" />
  </channel>
  <channel id="Rai 2">
    <display-name lang="it">Rai 2</display-name>
    <icon src="http://www.my-website.com/logos/Rai_2.png"/>
  </channel>
  <channel id="Rai 2 +1HD">
    <display-name lang="it">Rai 2 +1HD</display-name>
    <icon src="http://www.my-website.com/logos/Rai_2_+1HD.png"/>
  </channel>
  <channel id="Rai 2 +2HD">
    <display-name lang="it">Rai 2 +2HD</display-name>
    <icon src="http://www.my-website.com/logos/Rai_2_+2HD.png"/>
  </channel>
.
.
.
.

Is important the link http have one name without blank space.

I am trying to do it with xmlstarlet, but I am totally beginner about scripting and I can't find a solution. :frowning:

How about

awk '
        {print " <channel id=\"" $0 "\">"
         print "   <display-name lang=\"it\">" $0 "</display-name>"
         gsub (/ /, "_")
         print "   <icon src=\"http://www.my-website.com/logos/" $0 ".png\" />"
         print " </channel>"
        }
' file
1 Like

Another solution using bash shell:

#!/bin/bash

while read chan
do
    printf "<channel id=\"%s\">\n" "$chan"

    printf "    <display-name lang="it">%s</display-name>\n" "$chan"
    printf "    <icon src=\"http://www.my-website.com/logos/%s.png\" />\n"  \
        "${chan// /_}"
    printf "</channel>\n"
done < infile
1 Like

Really thank you about both solutions, I need check the best one for me :smiley: :smiley: Anyway this is the only one forum where there is alsways someone ready to help!