Search the content of a file

Discussion in 'Perl Misc' started by anton.vandersteen@chello.nl, Mar 4, 2007.

  1. Guest

    Hello Perl addicts,

    This is my sollution:

    #!/perl/bin/perl
    #This programme is written by Anton van der Steen
    #Email adres:
    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.....
    , Mar 4, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J.Edwards
    Replies:
    0
    Views:
    1,666
    J.Edwards
    Jan 20, 2006
  2. TheKeith
    Replies:
    20
    Views:
    106,378
    Chris Morris
    Oct 29, 2003
  3. hazz
    Replies:
    6
    Views:
    49,532
    SkyUCHC
    Jun 9, 2010
  4. News.Individual.NET

    search in tgz file content

    News.Individual.NET, Oct 13, 2004, in forum: Python
    Replies:
    2
    Views:
    345
    Robby Russell
    Oct 13, 2004
  5. Abby Lee
    Replies:
    5
    Views:
    393
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page