Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[risolto] script avanzato parsing testo
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Forum italiano (Italian) Forum di discussione italiano
View previous topic :: View next topic  
Author Message
sopadj1
n00b
n00b


Joined: 06 Sep 2005
Posts: 27

PostPosted: Wed Jan 24, 2007 9:06 pm    Post subject: [risolto] script avanzato parsing testo Reply with quote

salve gente!

non mi sto a dilungare spiegando la vicenda e le urgenze, arrivo subito al dunque:

ho un file di testo con n righe cosi formattato:

Code:

........
mysql -u root -pgood -D robin_www --exec "update dartagnan set user_id ='9009' where username =pippo'"
mysql -u root -pgood -D robin_www --exec "update dartagnan set user_id ='9010' where username ='pluto'"
mysql -u root -pgood -D robin_www --exec "update dartagnan set user_id ='9011' where username ='paperino"
mysql -u root -pgood -D robin_www --exec "update dartagnan set user_id ='blablabla' where username ='minni'"
mysql -u root -pgood -D robin_www --exec "update dartagnan set user_id ='bliblibli' where username ='topolino'"
.......


come potete notare, questo file è composto da n record, omogenei, dove l' unica cosa che cambia è il numero id (numerico e crescente) e l'username.

il problema nasce dal fatto che ad un certo punto gli id non sono + campi numerici e crescenti, benzi parole alla rinfusa!

io vorrei uno script che mi sostituisca, quando il campo degli id fa a farsi fottere (precisamente dopo il 9011), con il numero seguente!

nel nostro esempio qindi blablabla diventera' 9012, bliblibli diventera 9013 e cosi via!

contanto che i record sono centinaia e centinaia, farlo a mano sarebbe da suicidio!

chi mi salva la vita, il lavoro e il tunnel carpale?
_________________
http://sopadj1.netsons.org


Last edited by sopadj1 on Thu Jan 25, 2007 1:21 pm; edited 1 time in total
Back to top
View user's profile Send private message
gioi
Apprentice
Apprentice


Joined: 14 Feb 2006
Posts: 236

PostPosted: Wed Jan 24, 2007 9:38 pm    Post subject: Reply with quote

prova questo:

script.pl:
Code:

#!/usr/bin/perl

@LIST=<STDIN>;

foreach $LINE (@LIST){if($LINE =~ m/\'(\d+)\'/){$LAST=$LINE;$LAST =~ s/.+\'(\d+)\'.+/$1/;$LAST++;}else{$LINE =~ s/\'\w+\'/\'$LAST\'/;$LAST++;}print $LINE; }


usalo così:

Code:
cat nome_file_lista | ./script.pl
Back to top
View user's profile Send private message
sopadj1
n00b
n00b


Joined: 06 Sep 2005
Posts: 27

PostPosted: Wed Jan 24, 2007 9:44 pm    Post subject: Reply with quote

momentaneamente non ho accesso al server, ma non leggo in questo algoritmo l' istruzione che dice che deve iniziare il lavoro dopo il numero 9011!

il file non comincia con record che hanno user_id = 1!

che ne dici che prendo i record che sono corrotti (dal 9011 in poi) e li metto in un nuovo file, e nel ciclo dargli l' istruzione che deve partire da 9012 in poi?
_________________
http://sopadj1.netsons.org
Back to top
View user's profile Send private message
syntaxerrormmm
Guru
Guru


Joined: 17 Apr 2005
Posts: 449
Location: Romano di Lombardia - BG - ITALY

PostPosted: Wed Jan 24, 2007 9:54 pm    Post subject: Reply with quote

Guarda che il codice funziona (oddio, andrebbe un pochino aggiustato). Non vedi istruzioni perché non conosci le Regular Expressions.

Un'altra cosa: puoi inserire un elenco di istruzioni SQL in un unico file di testo e poi darlo in pasto al DB con:
Code:
mysql -u root -pgood robin_www < script.sql


[Edit]
Giusto per la cronaca, ecco come avrei 'aggiustato' lo script':
Code:
#!/usr/bin/perl
use warnings;
use strict;

my $last;

foreach (<STDIN>) {
    if (/\'\d+\'/) {
        $last = $_;
        $last =~ s/.+\'(\d+)\'.+/$1/;
    } else {
        $_ =~ s/\'\w+\'/\'$last\'/;
    }
   
    $last++;
    print $_;
}
Che, più semplicemente, si può lanciare così:
Code:
./prova.pl < input.sh > output.sh
[/Edit]

Ciao.
_________________
"Every rule can be broken, but no rule may be ignored."

Last.fm - Gentoo 2006.1 on:
Acer Aspire 1355LC - suspend2-sources-2.6.18-r3
IBM Thinkpad T30 - suspend2-sources-2.6.19-r3
Back to top
View user's profile Send private message
cloc3
Advocate
Advocate


Joined: 13 Jan 2004
Posts: 4793
Location: http://www.gentoo-users.org/user/cloc3/

PostPosted: Wed Jan 24, 2007 10:09 pm    Post subject: Reply with quote

pure gli esperti vuoi... :roll:

io sono uno fichissimo, se mi scrivi in pm, ti posto soluzione e parcella...

va bon. sono anche arrivato secondo. mi scocciano un poco quegli apici, comunque anche una cosa così viene veloce:
Code:

cloc3@wlan0 ~/prova $ cat prova
mysql -u root -pgood -D robin_www --exec "update dartagnan set user_id ='9010' where username ='pluto'"
mysql -u root -pgood -D robin_www --exec "update dartagnan set user_id ='9011' where username ='paperino'"
mysql -u root -pgood -D robin_www --exec "update dartagnan set user_id ='blablabla' where username ='minni'"
mysql -u root -pgood -D robin_www --exec "update dartagnan set user_id ='bliblibli' where username ='topolino'"
cloc3@wlan0 ~/prova $ awk -F"'" '{ print $1"%"FNR"%"$3"%"$4"%"$5}' prova |sed "s/%/'/g"
mysql -u root -pgood -D robin_www --exec "update dartagnan set user_id ='1' where username ='pluto'"
mysql -u root -pgood -D robin_www --exec "update dartagnan set user_id ='2' where username ='paperino'"
mysql -u root -pgood -D robin_www --exec "update dartagnan set user_id ='3' where username ='minni'"
mysql -u root -pgood -D robin_www --exec "update dartagnan set user_id ='4' where username ='topolino'"

_________________
vu vu vu
gentù
mi piaci tu
Back to top
View user's profile Send private message
gioi
Apprentice
Apprentice


Joined: 14 Feb 2006
Posts: 236

PostPosted: Wed Jan 24, 2007 10:17 pm    Post subject: Reply with quote

sopadj1 wrote:
momentaneamente non ho accesso al server, ma non leggo in questo algoritmo l' istruzione che dice che deve iniziare il lavoro dopo il numero 9011!

il file non comincia con record che hanno user_id = 1!

che ne dici che prendo i record che sono corrotti (dal 9011 in poi) e li metto in un nuovo file, e nel ciclo dargli l' istruzione che deve partire da 9012 in poi?


Il mio script automaticamente inizia dal primo record non numerico, e lo sostituisce con l'ultimo record numerico incrementato di 1 ogni volta...

quindi... che parta dall'1 o dal 9011 non cambia nulla!
Back to top
View user's profile Send private message
sopadj1
n00b
n00b


Joined: 06 Sep 2005
Posts: 27

PostPosted: Wed Jan 24, 2007 11:31 pm    Post subject: Reply with quote

grazie a tutti!

problema risolto! :*
_________________
http://sopadj1.netsons.org
Back to top
View user's profile Send private message
cloc3
Advocate
Advocate


Joined: 13 Jan 2004
Posts: 4793
Location: http://www.gentoo-users.org/user/cloc3/

PostPosted: Thu Jan 25, 2007 1:10 pm    Post subject: Reply with quote

sopadj1 wrote:
grazie a tutti!

problema risolto! :*

ok. per favore, segnala nel titolo ... e magari cambia anche il tag introduttivo, che è un po' perentorio, perché suona tanto : "caro helpdesk, raffreddami la patata".

:)
_________________
vu vu vu
gentù
mi piaci tu
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Forum italiano (Italian) Forum di discussione italiano All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum