#!/usr/bin/ruby -w #======================================================= # txt2discos.rb - transforma texto en registros SQLite3 # ------------------------------------------------------ # Los registros en el archivo discos.txt deben estar # separados por una línea que contenga el signo + # Por ejemplo: # ------------------------------------------------------ # TATUM_HINES_WILSON # THE SWING PIANO # CINTA # V # 1977 # US0001 # ETC # ETC # PRIMER REGISTRO GRABADO # + # ------------------------------------------------------ # Cada línea es un campo: autor, ndisco, tipo, gro # ae, codigo, tca1, tca2, ob #======================================================= # FJA - neocipres@gmail.com Septiembre de 2008 #======================================================= begin c = 0 campos = [] regs = [] nomf = ARGV[0] tipo = nomf.include?('.txt') raise if !tipo open(nomf, 'r') do |f| f.each_line do |linea| # f.readlines genera una matriz, que debemos recoger lineas = f.readlines y recorrer lineas.each linea = linea.gsub(/['"]/,' ') # quitamos las comillas que puedan existir if !linea.include?('+') campos << linea.chop! if c < 9 c += 1 else c = 0 regs << campos campos = [] end end end regs.each do |reg| print "# ", reg.join(' C> '), "\n" end puts "\n Registros: #{regs.size} \n" # La DB discos.db la creamos la primera vez con: # $ sqlite3 discos.db "create table t1 (t1key INTEGER PRIMARY KEY, autor TEXT, ndisco TEXT, tipo TEXT, gro TEXT, ae TEXT, codigo TEXT, tca1 TEXT, tca2 TEXT, ob TEXT);" # Introducimos registros: regs.each do |reg| cmd = '' v_autor, v_ndisco, v_tipo, v_gro, v_ae, v_codigo, v_tca1, v_tca2, v_ob = reg cmd << %{sqlite3 discos.db "insert into t1 (autor, ndisco, tipo, gro, ae, codigo, tca1, tca2, ob) values ('#{v_autor.upcase}','#{v_ndisco.upcase}','#{v_tipo.upcase}','#{v_gro.upcase}','#{v_ae}','#{v_codigo.upcase}', '#{v_tca1}','#{v_tca2}','#{v_ob}');"} ok = system(cmd) if !ok puts cmd raise end end rescue if ARGV.size == 0 print "\n\t", "="*65 print "\n\t Uso: txt2discos <discos.txt> \n" print "\n\t\t \n " print "\n\t\t \n " print "\n\t\t \n " print "\t", "="*65, "\n\n" elsif !tipo print "\n\t el archivo debe ser del tipo .txt #{$!} \n\n" else print "\n\t Error-> #{$!} \n\n" end end