Convertir archivo csv a xml con sed
alphon5o
-
Usuario anónimo -
Usuario anónimo -
Bonjour,
dispondrás de un archivo csv en el que tengo muchas radios en esta forma:
quiero poder importarlas en un archivo xml para rhythmbox en esta forma:
ejemplo:
gracias de antemano
Configuración: Linux / Firefox 5.0
dispondrás de un archivo csv en el que tengo muchas radios en esta forma:
nombre_radio;dirección_del_flux_de_la_radio;
quiero poder importarlas en un archivo xml para rhythmbox en esta forma:
<entry type="iradio"> <title>nombre_radio</title> <genre></genre> <artist></artist> <album></album> <location>dirección_del_flux_de_la_radio</location> <play-count></play-count> <last-played></last-played> <bitrate></bitrate> <date></date> <mimetype>application/octet-stream</mimetype> </entry>
ejemplo:
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> .... ....
gracias de antemano
Configuración: Linux / Firefox 5.0
3 respuestas
-
En mi opinión, no ha emprendido nada...
Para la respuesta (¡de todas formas!):
Estado 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;
Primero, cambia el delimitador del archivo reemplazando los ; por espacios: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
Luego, creamos un pequeño script shell que se encargará de todo el trabajo:#!/bin/bash # csv2xml.sh : Transformación de archivo csv a archivo xml para integrarlo # en 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
Hacemos que el script sea ejecutable con un pequeño chmod:
franzux@maerix:~test$chmod +x csv2xml.sh
Solo queda ejecutar el script con nuestro archivo base como argumento:
franzux@maerix:~/test$ ./csv2xml.sh radio.csv
Y observar el resultado:
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>
Atentamente,
Franzux.
Intel Q6600 Debian Lenny//Gentoo
Bajo Linux, el 99% de los errores se encuentran entre el teclado y la silla... -
Personalmente, optaría más por un archivo de plantilla...
$ 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> $
--
Tranquiliza mis nervios ;-)
Haga un gesto por el medio ambiente, cierre sus ventanas y adopte un pingüino. -
Hola,
¿Y ???
¿Estás esperando a que te lo hagan todo o ya has emprendido algo?
--
Tranquilo mis nuggets ;-)
Haced un gesto por el medio ambiente, cerrad vuestras ventanas y adoptad un pingüino.