Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Search the content of a file

Reply
Thread Tools

Search the content of a file

 
 
anton.vandersteen@chello.nl
Guest
Posts: n/a
 
      03-04-2007
Hello Perl addicts,

This is my sollution:

#!/perl/bin/perl
#This programme is written by Anton van der Steen
#Email adres: http://www.velocityreviews.com/forums/(E-Mail Removed)
use Tk;
use File::Find;

my $mw = new MainWindow; # Main Window
$mw->title("Search Engine Version 2.0 by Stone Logic Systems");


################################################## ####################
$mw->configure(-menu => my $menubar = $mw->Menu);

my $filemenu = $menubar->cascade(-label => "~File",
-tearoff => 1);
my $execute_sql_statement= $menubar->cascade(-label => "Count
~Phrase",
-tearoff=>1);
my $save_result_to_file = $menubar->cascade(-label => "~Show Text",
-tearoff=>1);
my $clear_text_area = $menubar->cascade(-label => "~Clear Result Set",
-tearoff=>1);
my $create_pdf = $menubar->cascade(-label => "~Export to File",
-tearoff=>1);
my $create_excel = $menubar->cascade(-label=> "Find File",
-tearoff=>1);

#my $create_xml = $menubar->cascade(-label=> "Export to XML",
# -tearoff=>1);

my $helpmenu = $menubar->cascade(-label => "~Help",
-tearoff => 1);



$filemenu->command(-label => "E~xit",
-command => sub{$mw->destroy});

$execute_sql_statement->command(-label => "Count ~Phrase",
-command=> sub{push_button1()});

$save_result_to_file->command(-label =>"~Show Text",
-command=> sub{push_button2()});

$clear_text_area->command(-label => "~Clear Result Set",
-command=> sub {push_button3()});

$create_pdf->command(-label => "~Export to File",
-command=> sub {push_button4()});

$create_excel->command(-label =>"Find File",
-command=> sub {push_button5()}) ;

#$create_xml->command(-label =>"Export to XML",
#-command=> sub {XML()});

$helpmenu->command(-label => "~Help Contents",
-command => sub{showhelp()});

################################################## ####################
my $frm_name = $mw -> Frame() -> pack();

my $lab1 = $frm_name -> Label(-text=>"Phrase :", -font => '-adobe-
helvetica-bold-r-normal--11-120-75-75-p-70-*-1') -> pack();

my $ent1 = $frm_name -> Entry(-width=>100, -borderwidth=>2) -> pack();
$ent1->configure(-font => '-adobe-helvetica-bold-r-
normal--11-120-75-75-p-70-*-1');


my $lab2=$frm_name->Label(-text=>"Search in File :", -font => '-adobe-
helvetica-bold-r-normal--11-120-75-75-p-70-*-1')->pack();

my $ent2=$frm_name->Entry(-width=>100)->pack();
$ent2->configure(-font => '-adobe-helvetica-bold-r-
normal--12-120-75-75-p-70-*-1');

my $lab3=$frm_name->Label(-text=>"Export to file :", -font => '-adobe-
helvetica-bold-r-normal--11-120-75-75-p-70-*-1')->pack();

my $ent3=$frm_name->Entry(-width=>100)->pack();
$ent3->configure(-font => '-adobe-helvetica-bold-r-
normal--12-120-75-75-p-70-*-1');


#my $but1 = $mw -> Button(-text=>"Count Appearance Phrase", -command =>
\&push_button1, -background=>"green",
# -font => '-adobe-helvetica-bold-r-normal--11-120-75-75-p-70-*-1') ->
pack();

#my $but2 = $mw -> Button(-text=>"Show text", -command =>
\&push_button2, -background=>"yellow",
# -font => '-adobe-helvetica-bold-r-normal--11-120-75-75-p-70-*-1') ->
pack();

#my $but3 = $mw -> Button(-text=>"Clear Text Area", -command =>
\&push_button3, -background=>"orange",
# -font => '-adobe-helvetica-bold-r-normal--11-120-75-75-p-70-*-1') ->
pack();

#my $but4 = $mw -> Button(-text=>"Save result to file", -command =>
\&push_button4, -background=>"cyan",
# -font => '-adobe-helvetica-bold-r-normal--11-120-75-75-p-70-*-1') ->
pack();



#Text Area
my $txt = $mw->Scrolled( 'Text' , -scrollbars=>'se' , -wrap=>
'none',);
$txt->configure(-width=>120, -height=>20, -font => '-adobe-helvetica-
bold-r-normal--14-120-75-75-p-70-*-1');
$txt->pack();

MainLoop;

sub push_button1 {

use Getopt::Std;

my $name1 = $ent1 -> get();
my $name2 = $ent2 -> get();
@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 "The phrase $pattern is $i times found!!\n";


$txt-> insert ('0.0',"The phrase $pattern is $i times found in file
$file.\n");
close (FILE);
$i=0;

}


sub push_button2 {

use Getopt::Std;

my $name1 = $ent1 -> get();
my $name2 = $ent2 -> get();
@ARGV= ($name1, $name2);

$i=0;

my $pattern = shift @ARGV;


foreach $file (@ARGV)
{
open (FILE, $file);
while ($line = <FILE>)
{
if ($line =~m"$pattern")
{
$i++;
last if ($opt_1);
$txt-> insert ('end', "$line\n");
#print "$line\n";

}

}

close (FILE);
$i=0;

}

}


sub push_button3 {
$txt-> delete ('0.0', 'end');


}

sub push_button4 {

use Getopt::Std;

my $name1 = $ent1 -> get();
my $name2 = $ent2 -> get();
my $file_out = $ent3 -> get();

@ARGV= ($name1, $name2);

$i=0;

my $pattern = shift @ARGV;

open(OUT,">$file_out");


foreach $file (@ARGV)
{
open (FILE, $file);
while ($line = <FILE>)
{
if ($line =~m"$pattern")
{
$i++;
last if ($opt_1);
print OUT "$line\n";

}

}

close (FILE);
$i=0;

}
close(OUT);

}
}

sub push_button5{

find(\&push_button6, "c:\\")



}


sub push_button6 {

use Getopt::Std;
use File::Find;

my $a = $ent1 -> get();



if (/\.$a$/) # ((/\.zip$/) ||
{ #print "$File::Find::name\n";
$txt-> insert ('end', "$File::Find::name\n");
}
}

;


Have fun.....

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error: Only Content controls are allowed directly in a content page that contains Content controls. hazz ASP .Net 6 06-09-2010 01:54 PM
Error: "Content controls can only be used in a content page" J.Edwards ASP .Net 0 01-20-2006 11:43 AM
search in tgz file content News.Individual.NET Python 2 10-13-2004 04:08 PM
search within a search within a search - looking for better way...my script times out Abby Lee ASP General 5 08-02-2004 04:01 PM



Advertisments