Perl script for scanning files

  • Thread starter Anton van der Steen
  • Start date
A

Anton van der Steen

Hey 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 = <FILE>)
{
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 = <FILE>)
{
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');


}

};
 
R

Raghuramaiah Gompa

Tk *is* standard in ActiveState's distribution.

Paul Lalli

I wonder whether this code can be modifed to "highlight
all instances of text between quotes". For example, if
the previous
sentence is in a file, the code should show the line with
red color for "highlight all instances of text between
quotes". Is it possible? .. Raghu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top