Convertir fichier csv en xml avec sed

Bonjour,

je dispose d'un fichier csv dans le quel j'ai plein de radios sous cette forme:
nom_radio;adresse_du_flux_de_laradio;

je souhaite pouvoir les importer donc dans un fichier xml pour rhytmbox sous cette forme:
  
<entry type="iradio">  
    <title>nom_radio</title>  
    <genre></genre>  
    <artist></artist>  
    <album></album>  
    <location>adresse_du_flux_de_laradio</location>  
    <play-count></play-count>  
    <last-played></last-played>  
    <bitrate></bitrate>  
    <date></date>  
    <mimetype>application/octet-stream</mimetype>  
  </entry>

exemple:
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_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>  
....  
....  


merci d'avance

3 réponses

  • Votes
  • Date
  • Date décroissante
  1. Contributeur
    À mon avis, il n'a rien entrepris...

    Pour la réponse (quand même !) :

    État de base :
    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;    
    


    D'abord, change le délimiteur du fichier afin de remplacer les ; par des espaces :
    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  


    Ensuite, on créé un petit script shell qui va se charger de tout le boulot :
    #!/bin/bash 
    # csv2xml.sh : Transformation de fichier csv en fichier xml afin de l'intégrer 
    # à Rhytmbox 
    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 
    


    On rend le script exécutable avec un petit chmod :

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


    Ne reste qu'à lancer le script avec notre fichier de base comme argument :

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


    Et à observer le résultat :

    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>  
    


    Cordialement,

    Franzux.
    Intel Q6600 Debian Lenny//Gentoo
    Sous Linux, 99% des bugs se situent entre le clavier et la chaise de bureau...
    1. Contributeur
      Perso j'opterai plus pour un fichier template...

      $ cat template.txt 
      <entry type="iradio">
          <title>NOM</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
      NOM="${line%;*}"
      URL="${line#*://}"
      sed "s#NOM#${NOM}#;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>
      $
      1. Contributeur
        Pas mal, je n'y pense jamais ;)
      2. Moi je dis chapeau à vous deux, jamais je n'arriverais à faire des trucs comme ça !! :P
        De plus, je trouve quand même très drôle de voir qu'à toute les fois quelqu'un à une meilleur façons de faire que l'autre et que comme ça vous êtes capable de pondre plusieurs scripts !! : )
    2. Contributeur
      Salut,

      Et ???

      T'attends qu'on te fasse tout ou tu as déjà entrepris quelque chose ?