PDA

View Full Version : Da file di testo a xls


dAbOTZ
18-12-2010, 20:05
Hola. Ho un file di testo così composto:


IAD: 1
IP: 101.84.187.66
FIRMWARE: 4.0.16
MODELLO: SV1242
IAD: 2
IP: 101.58.8.83
FIRMWARE: 4.0.18
MODELLO: SV1242
IAD: 3
IP: 101.84.186.83
IAD: 4
IP: 101.78.208.2
IAD: 5
IP: 101.84.43.139
MODELLO: ViNE2100
FIRMWARE: 3.2.1g
IAD: 6
IP: 101.84.20.13
FIRMWARE: 4.0.16
MODELLO: SV1242
IAD: 7
IP: 101.78.24.228
IAD: 8
IP: 101.69.169.235
MODELLO: ViNE1000
FIRMWARE: 2.3.4b
IAD: 9
IP: 101.58.8.194
FIRMWARE: 4.0.18
MODELLO: SV1242


Ad libitum fino a 10mila e rotti. Devo generarne un altro file che a sua volta sarà poi importato in excel, così composto:


IAD IP MODELLO FIRMWARE

1 101.84.187.66 SV1242 4.0.16
2 101.58.8.83 SV1242 4.0.18
3 101.84.186.83 DOWN DOWN
4 101.78.208.2 DOWN DOWN
5 101.84.43.139 ViNE2100 3.2.1g
6 101.84.20.13 SV1242 4.0.16
7 101.78.24.228 DOWN DOWN
8 101.69.169.235 ViNE1000 2.3.4b
9 101.58.8.194 SV1242 4.0.18


Problema: i router down non sono stati tracciati e la relativa riga con modello e firmware è inesistente nel file. Inoltre le righe con MODELLO e FIRMWARE sono spesso invertite.

Contate che, importazione in excel a parte, devo poter fare tutto da shell. Idee?

wizard1993
18-12-2010, 22:15
è strettamente necessario che tu esporti in formato xls o ti puoi accontentare di un più semplice csv?
Oltre alla shell (bash immagino), cos'altro hai a disposizione? che ne so, un interprete python?

dAbOTZ
18-12-2010, 23:04
L'xls verrà poi, csv andrebbe benone. Python all'occorrenza posso installarlo, come più o meno qualunque altro strumento utilizzabile via shell.

wizard1993
18-12-2010, 23:19
se puoi usare python, ho improvvisato uno scriptino che dovrebbe fare al caso tuo.
la qualità è bassa (è il primo script in python che faccio) e anche contando l'ora...
Comunque ecco a te
import sys
class router:
attributi={"IAD":""}
print "log parser"
if(len(sys.argv)<2):
print "missing input file"
exit()
else:
print len(sys.argv)
fInput=open(sys.argv[1],"r")
if(len(sys.argv)==2):
fOutput=sys.stdout
else:
fOutput=open(sys.argv[2],"w")


routers =[]
def printToFile(r):
if r.attributi["IAD"]=="":
return
str=r.attributi["IAD"]+"\t"+r.attributi["IP"]+"\t";
if r.attributi.has_key("MODELLO"):
str+= r.attributi["MODELLO"]
else:
str+= "DOWN"
str+="\t"
if r.attributi.has_key("FIRMWARE"):
str+= r.attributi["FIRMWARE"]
else:
str+= "DOWN"
str+="\t"
fOutput.write(str+"\n")
def load():
r= router()
while True:
testo=fInput.readline()
if testo =="":
printToFile(r)
break
index=testo.find(":");
intestazione=testo[:index]
dato=testo[index+1:len(testo)-1]
#print intestazione,"-->",dato
if intestazione=="IAD":
printToFile(r);
r=router()
r.attributi={}
#print "\n---\n"
r.attributi[intestazione]=dato
load()


lo script carica i dati dal file passato come primo parametro e li spara dentro il file passato come secondo parametro.Se questo non è specificato li spara sullo standard output
Ho usato come file di esempio quello da te postato e funziona

dAbOTZ
19-12-2010, 13:13
Funziona egregiamente, proprio quel che mi serviva. Grande, ti devo una birra.

wizard1993
19-12-2010, 14:21
di nulla, se ti serve altro non hai che da chiedere