Reply to topic
Perl : grep
night_scorpion


Joined: 12 Jul 2006
Posts: 3
Reply with quote
Hello,

I am stuck with my following code with which i am not able to go further. I would be gratefull if anybody could help me out with it.

i have a text file tempsql.lst(2343231,45642342,34534534.....)which contains my UPDATED stocklist values and under the doku directory i have many sql files (2111.sql,2112.sql...)which contains insert statements
INSERT INTO temp VALUES (1,2342342);
INSERT INTO temp VALUES (1,2343442);....
where 2342342,2343442.. are also my stocklist values(but NOT the updated values ie from my old database ). Now i need to check which of the values from tempsql.lst are not there in ALL of the .sql files from my doku directory and print these stocklist values out.

From this program wht i have written it gives me all the values from tempsql.lst but i dont come to the proper output...i have also tried with the module fgrep but it didnt help me out much.


#!/usr/bin/perl -w

open(VALUE,"< tempsql.lst");
@stock = <VALUE>;

opendir(DIR,"../rc_stocklist/doku");

@filename = grep(/\.sql$/,readdir(DIR));
closedir(DIR);

foreach $m (@stock)
{
foreach $l (@filename)
{
open(FILE,"../rc_stocklist/doku/$l");
@inserts = <FILE>;
# print "grep($m , @inserts) ";
@found =( grep ($m , @inserts));
$elements = @found;
if ($elements== 0)
{
print $m;
}
}
}
close VALUE;


thank u
spoulson


Joined: 21 Jun 2006
Posts: 22
Location: Middletown, DE
Reply with quote
Your search criteria doesn't seem to be certain. Do you need the stock values to be found in all the .sql files, or does it need to be in just one? I'm not sure what these doku sql files are supposed to accomplish.

What is the expected outcome that you're not getting?
night_scorpion


Joined: 12 Jul 2006
Posts: 3
Reply with quote
Do you need the stock values to be found in all the .sql files, or does it need to be in just one?


yes i need to check each of the stock values from the tempsql.lst(updated list) in all of the .sql files (old list)

i have modified the file

as for the output i am getting all the values from tempsql.lst as an output which means it does not find the respected value. i guess the problem is somewhere near the grep function itself(maybe with empty spaces) for which i am not able to find it out .. i also tried to debug the program to check it out wht the pro was ..but it didnt help me much .

#!/usr/bin/perl -w

sub strip($)
{
my $string = shift;
chomp($string);
$string =~ s/^\s+//; # remove leading whitespace
$string =~ s/\s+$//; # remove trailing whitespace return
$string;
}

open(VALUE,"< tempsql.lst");
@stock = <VALUE>;
close VALUE;

opendir(DIR,"../rc_stocklist/doku");

@filename = grep(/\.sql$/,readdir(DIR));
closedir(DIR);


foreach $m (@stock)
{
strip($m); # Added here
foreach $l (@filename)
{

@found =( grep (/$m/ ,"../rc_stocklist/doku/$l")); #Changed here
}
$elements = @found;
if ($elements ==0)
{
print $m;
}

}




as an eg i am posting here the values of tempsql.lst
75000169
840001749
75000168
840001750
840001751
840001752
840001753
840001754
840001755
840001756
840001757
840001758
840001759
840001732
840001733
840001734
840001735
840001736
840001737
840001738
840001739
840001740
840001571
840001572
840001573
840001574
98211401
840001575
98211402
8211107
840001576
98211404
75000143

and 2 sql files (2111.sql)
INSERT INTO product.p_tmpr VALUES (1,98211101);
INSERT INTO product.p_tmpr VALUES (2,98211102);
INSERT INTO product.p_tmpr VALUES (3,98211419);
INSERT INTO product.p_tmpr VALUES (4,984222011);
INSERT INTO product.p_tmpr VALUES (5,8211105);
INSERT INTO product.p_tmpr VALUES (6,98211106);
INSERT INTO product.p_tmpr VALUES (7,984222046);
INSERT INTO product.p_tmpr VALUES (8,9840001811);
INSERT INTO product.p_tmpr VALUES (9,98211107);
INSERT INTO product.p_tmpr VALUES (10,98211108);
INSERT INTO product.p_tmpr VALUES (11,984222029);
INSERT INTO product.p_tmpr VALUES (12,984222045);
INSERT INTO product.p_tmpr VALUES (13,9840001812);
INSERT INTO product.p_tmpr VALUES (14,98211205);
INSERT INTO product.p_tmpr VALUES (15,98211206);
INSERT INTO product.p_tmpr VALUES (16,98211207);
INSERT INTO product.p_tmpr VALUES (17,840001755);
INSERT INTO product.p_tmpr VALUES (18,840001756);
INSERT INTO product.p_tmpr VALUES (19,9840001757);
INSERT INTO product.p_tmpr VALUES (20,9840001758);
INSERT INTO product.p_tmpr VALUES (21,984222026);
INSERT INTO product.p_tmpr VALUES (22,984222032);
INSERT INTO product.p_tmpr VALUES (23,984222025);
INSERT INTO product.p_tmpr VALUES (24,984222019);
INSERT INTO product.p_tmpr VALUES (25,9840001804);
INSERT INTO product.p_tmpr VALUES (26,98211114);

and (2112.sql)
INSERT INTO product.p_tmpr VALUES (1,98211302);
INSERT INTO product.p_tmpr VALUES (2,840001757);
INSERT INTO product.p_tmpr VALUES (3,984222043);
INSERT INTO product.p_tmpr VALUES (4,984222033);
INSERT INTO product.p_tmpr VALUES (5,984222048);
INSERT INTO product.p_tmpr VALUES (6,984222047);
INSERT INTO product.p_tmpr VALUES (7,982113011);

however for my projekt there are many sql files ..

i hope that this information is enough.. to perhaps try it out Confused

thank u
Perl : grep
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
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic