Perl script for scanning files
am 02.12.2005 19:42:20 von Anton van der SteenHey guys,
Here a script for searching for words or phrases in files.
The lines cna be shown with one press on the button.
I have tested this program on a text file of 2 Gb and it took 8 minutes to
present a result scannin on the letter 'e'.
I use Perl from www.ActiveState.com .
No extra Perl Modules have to be installed.
Success and have fun with. I use it a lot.
Scoon
#!/perl/bin/perl
use Tk;
my $mw = new MainWindow; # Main Window
my $frm_name = $mw -> Frame() -> pack();
my $lab1 = $frm_name -> Label(-text=>"Phrase :") -> pack();
my $ent1 = $frm_name -> Entry(-width=>100) -> pack();
my $lab2=$frm_name->Label(-text=>"File :")->pack();
my $ent2=$frm_name->Entry(-width=>100)->pack();
my $but1 = $mw -> Button(-text=>"Count Appearance Phrase", -command
=>\&push_button1) -> pack();
my $but2 = $mw -> Button(-text=>"Show text", -command =>\&push_button2) ->
pack();
my $but3 = $mw -> Button(-text=>"Clear Text Area", -command
=>\&push_button3) -> pack();
#Text Area
my $txt = $mw -> Text(-width=>130, -height=>40) -> pack();
MainLoop;
sub push_button1 {
use Getopt::Std;
my $name1 = $ent1 -> get();
my $name2 = $ent2 -> get();
#$txt -> insert('end',"Hello, $name1, $name2");
@ARGV= ($name1, $name2);
#print @ARGV;
$i=0;
my $pattern = shift @ARGV;
foreach $file (@ARGV)
{
open (FILE, $file);
while ($line =
{
if ($line =~m"$pattern")
{
$i++;
last if ($opt_1);
#print "Het woord is gevonden in het document\n";
#print "$file: $line\n" if (!$opt_1);
#$txt-> insert ('end', "$file, $line");
}
}
#print "$file" if ($opt_1);
print "The phrase $pattern is $i times found!!\n";
$txt-> insert ('0.0',"The phrase $pattern is $i times found in file
$file.");
#$txt-> insert ('0.0', "$file, $line");
close (FILE);
$i=0;
}
sub push_button2 {
use Getopt::Std;
my $name1 = $ent1 -> get();
my $name2 = $ent2 -> get();
#$txt -> insert('end',"Hello, $name1, $name2");
@ARGV= ($name1, $name2);
#print @ARGV;
$i=0;
my $pattern = shift @ARGV;
foreach $file (@ARGV)
{
open (FILE, $file);
while ($line =
{
if ($line =~m"$pattern")
{
$i++;
last if ($opt_1);
#print "Het woord is gevonden in het document\n";
#print "$file: $line\n" if (!$opt_1);
#$txt-> insert ('end', "$file, $line");
$txt-> insert ('end', "$line");
}
}
#print "$file" if ($opt_1);
#print "Het woord $pattern is $i keer gevonden\n";
#$txt-> insert ('end',"Het woord $pattern is $i keer gevonden in bestand
$file.");
#$txt-> insert ('end', "$file, $line");
close (FILE);
$i=0;
}
}
sub push_button3 {
$txt-> delete ('0.0', 'end');
}
};