Convert CSV file to XML with sed

alphon5o -  
 Anonymous user -
Bonjour,

I have a CSV file containing many radios in the following format:
radio_name;radio_stream_address;

I would like to import them into an XML file for Rhythmbox in this format:
 <entry type="iradio"> <title>radio_name</title> <genre></genre> <artist></artist> <album></album> <location>radio_stream_address</location> <play-count></play-count> <last-played></last-played> <bitrate></bitrate> <date></date> <mimetype>application/octet-stream</mimetype> </entry> 

Example:
myDesktop $ cat radio.csv nrj;http://player.nrj.fr/V4/nrj/nrj.asx; nrj_french;http://player.nrj.fr/V4/nrj/webradios/nrj_french.m3u; nrj_girl;http://player.nrj.fr/V4/nrj/webradios/nrj_girl.m3u; nrj_rap;http://player.nrj.fr/V4/nrj/webradios/nrj_rap.m3u; nrj_rnb;' target='_blank' rel='nofollow'>' target='_blank' rel='nofollow'>http://player.nrj.fr/V4/nrj/webradios/nrj_rnb.m3u; 


 myDesktop $ cat rb.xml <entry type="iradio"> <title>nrj</title> <genre></genre> <artist></artist> <album></album> <location>http://player.nrj.fr/V4/nrj/nrj.asx</location> <play-count></play-count> <last-played></last-played> <bitrate></bitrate> <date></date> <mimetype>application/octet-stream</mimetype> </entry> <entry type="iradio"> <title>nrj_french</title> <genre></genre> <artist></artist> <album></album> <location>http://player.nrj.fr/V4/nrj/webradios/nrj_french.m3u</location> <play-count></play-count> <last-played></last-played> <bitrate></bitrate> <date></date> <mimetype>application/octet-stream</mimetype> </entry> <entry type="iradio"> <title>nrj_girl</title> <genre></genre> <artist></artist> <album></album> <location>http://player.nrj.fr/V4/nrj/webradios/nrj_girl.m3u</location> <play-count></play-count> <last-played></last-played> <bitrate></bitrate> <date></date> <mimetype>application/octet-stream</mimetype> </entry> .... .... 


Thank you in advance

Configuration: Linux / Firefox 5.0

3 answers

  1. Franzux Posted messages 9705 Status Contributor 1 146
     
    In my opinion, he hasn't done anything...

    For the response (still!):

    Base state:
    franzux@maerix:~/test$ cat radio.csv nrj;http://player.nrj.fr/V4/nrj/nrj.asx; nrj_french;http://player.nrj.fr/V4/nrj/webradios/nrj_french.m3u; nrj_girl;http://player.nrj.fr/V4/nrj/webradios/nrj_girl.m3u; nrj_rap;http://player.nrj.fr/V4/nrj/webradios/nrj_rap.m3u; 


    First, change the delimiter of the file to replace the ; with spaces:
     franzux@maerix:~/test$ sed -i 's/;/ /g' radio.csv franzux@maerix:~/test$ cat radio.csv nrj http://player.nrj.fr/V4/nrj/nrj.asx nrj_french http://player.nrj.fr/V4/nrj/webradios/nrj_french.m3u nrj_girl http://player.nrj.fr/V4/nrj/webradios/nrj_girl.m3u nrj_rap http://player.nrj.fr/V4/nrj/webradios/nrj_rap.m3u 


    Next, we create a small shell script that will take care of everything:
    #!/bin/bash # csv2xml.sh : Transforming csv file to xml file for integration # into Rhythmbox filename=$1 while read line; do nom='echo ${line} | awk {'print $1'}' adresse='echo ${line} | awk {'print $2'}' echo -e "<entry type=\"iradio\">\n\t<title>$nom</title>\n\t<artist></artist>\n\t<album></album>\n\t<location>$adresse</location>\n\t<play-count></play-count>\n\t<last-played></last-played>\n\t<bitrate></bitrate>\n\t<date></date>\n\t<mimetype>application/octet-stream</mimetype>\n</entry>" done < $filename > rb.xml 


    We make the script executable with a small chmod:

    franzux@maerix:~test$chmod +x csv2xml.sh


    Just run the script with our base file as an argument:

    franzux@maerix:~/test$ ./csv2xml.sh radio.csv 


    And observe the result:

    franzux@maerix:~/test$ cat rb.xml <entry type="iradio"> <title>nrj</title> <artist></artist> <album></album> <location>http://player.nrj.fr/V4/nrj/nrj.asx</location> <play-count></play-count> <last-played></last-played> <bitrate></bitrate> <date></date> <mimetype>application/octet-stream</mimetype> </entry> <entry type="iradio"> <title>nrj_french</title> <artist></artist> <album></album> <location>http://player.nrj.fr/V4/nrj/webradios/nrj_french.m3u</location> <play-count></play-count> <last-played></last-played> <bitrate></bitrate> <date></date> <mimetype>application/octet-stream</mimetype> </entry> <entry type="iradio"> <title>nrj_girl</title> <artist></artist> <album></album> <location>http://player.nrj.fr/V4/nrj/webradios/nrj_girl.m3u</location> <play-count></play-count> <last-played></last-played> <bitrate></bitrate> <date></date> <mimetype>application/octet-stream</mimetype> </entry> <entry type="iradio"> <title>nrj_rap</title> <artist></artist> <album></album> <location>http://player.nrj.fr/V4/nrj/webradios/nrj_rap.m3u</location> <play-count></play-count> <last-played></last-played> <bitrate></bitrate> <date></date> <mimetype>application/octet-stream</mimetype> </entry> 


    Best regards,

    Franzux.
    Intel Q6600 Debian Lenny//Gentoo
    Under Linux, 99% of bugs are located between the keyboard and the office chair...
    1
  2. zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   6 501
     
    Personally, I would lean more towards a template file...

    $ cat template.txt  <entry type="iradio"> <title>NAME</title> <genre></genre> <artist></artist> <album></album> <location>http://URL</location> <play-count></play-count> <last-played></last-played> <bitrate></bitrate> <date></date> <mimetype>application/octet-stream</mimetype> </entry> $ cat radio.csv  nrj;http://player.nrj.fr/V4/nrj/nrj.asx; nrj_french;http://player.nrj.fr/V4/nrj/webradios/nrj_french.m3u; nrj_girl;http://player.nrj.fr/V4/nrj/webradios/nrj_girl.m3u; nrj_rap;http://player.nrj.fr/V4/nrj/webradios/nrj_rap.m3u; nrj_rnb;' target='_blank' rel='nofollow'>' target='_blank' rel='nofollow'>http://player.nrj.fr/V4/nrj/webradios/nrj_rnb.m3u; $ cat foo.sh  #! /bin/bash while read line do NAME="${line%;*}" URL="${line#*://}" sed "s#NAME#${NAME}#;s#URL#${URL}#" template.txt done < <(sed 's/;$//' radio.csv) $ ./foo.sh  <entry type="iradio"> <title>nrj</title> <genre></genre> <artist></artist> <album></album> <location>http://player.nrj.fr/V4/nrj/nrj.asx</location> <play-count></play-count> <last-played></last-played> <bitrate></bitrate> <date></date> <mimetype>application/octet-stream</mimetype> </entry> <entry type="iradio"> <title>nrj_french</title> <genre></genre> <artist></artist> <album></album> <location>http://player.nrj.fr/V4/nrj/webradios/nrj_french.m3u</location> <play-count></play-count> <last-played></last-played> <bitrate></bitrate> <date></date> <mimetype>application/octet-stream</mimetype> </entry> <entry type="iradio"> <title>nrj_girl</title> <genre></genre> <artist></artist> <album></album> <location>http://player.nrj.fr/V4/nrj/webradios/nrj_girl.m3u</location> <play-count></play-count> <last-played></last-played> <bitrate></bitrate> <date></date> <mimetype>application/octet-stream</mimetype> </entry> <entry type="iradio"> <title>nrj_rap</title> <genre></genre> <artist></artist> <album></album> <location>http://player.nrj.fr/V4/nrj/webradios/nrj_rap.m3u</location> <play-count></play-count> <last-played></last-played> <bitrate></bitrate> <date></date> <mimetype>application/octet-stream</mimetype> </entry> <entry type="iradio"> <title>nrj_rnb</title> <genre></genre> <artist></artist> <album></album> <location>http://player.nrj.fr/V4/nrj/webradios/nrj_rnb.m3u</location> <play-count></play-count> <last-played></last-played> <bitrate></bitrate> <date></date> <mimetype>application/octet-stream</mimetype> </entry> $

    --
    Zen my nuggets ;-)
    Do something for the environment, close your windows and adopt a penguin.
    1
    1. Franzux Posted messages 9705 Status Contributor 1 146
       
      Not bad, I never think about it ;)
      0
    2. Anonymous user
       
      I take my hat off to both of you, I could never pull off stuff like that!! :P
      Moreover, I still find it very funny to see that every time someone has a better way of doing things than the other and that this way you are able to churn out multiple scripts!! :)
      0
  3. zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   6 501
     
    Hi,

    So ???

    Are you waiting for us to do everything for you or have you already started something?

    --
    Zen my nuggets ;-)
    Make a gesture for the environment, close your windows and adopt a penguin.
    0