Perl script for scanning files

Discussion in 'Perl Misc' started by Anton van der Steen, Dec 2, 2005.

  1. 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');


    }

    };
     
    Anton van der Steen, Dec 2, 2005
    #1
    1. Advertising

  2. Anton van der Steen

    Guest

    Anton van der Steen wrote:
    > No extra Perl Modules have to be installed.


    > #!/perl/bin/perl
    > use Tk;


    FWIW, Tk is not a standard module (at least not in my Perl 5.8.4
    distribution).
     
    , Dec 2, 2005
    #2
    1. Advertising

  3. Anton van der Steen

    Paul Lalli Guest

    wrote:
    > Anton van der Steen wrote:
    > > No extra Perl Modules have to be installed.

    >
    > > #!/perl/bin/perl
    > > use Tk;

    >
    > FWIW, Tk is not a standard module (at least not in my Perl 5.8.4
    > distribution).


    Anton van der Steen wrote:
    > > I use Perl from www.ActiveState.com .


    Tk *is* standard in ActiveState's distribution.

    Paul Lalli
     
    Paul Lalli, Dec 2, 2005
    #3
  4. In article <>,
    Paul Lalli <> wrote:
    > wrote:
    >> Anton van der Steen wrote:
    >> > No extra Perl Modules have to be installed.

    >>
    >> > #!/perl/bin/perl
    >> > use Tk;

    >>
    >> FWIW, Tk is not a standard module (at least not in my

    >Perl 5.8.4
    >> distribution).

    >
    >Anton van der Steen wrote:
    >> > I use Perl from www.ActiveState.com .

    >
    >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
     
    Raghuramaiah Gompa, Jan 21, 2006
    #4
    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. dpackwood
    Replies:
    3
    Views:
    1,842
  2. Anthony Papillion II

    Perl, Email, and Scanning

    Anthony Papillion II, Nov 11, 2004, in forum: Perl
    Replies:
    1
    Views:
    476
    Gunnar Hjalmarsson
    Nov 11, 2004
  3. Matt G
    Replies:
    1
    Views:
    1,187
    Deepak Kumar Vasudevan
    Aug 22, 2003
  4. Brian Henry

    virus scanning uploaded files

    Brian Henry, Mar 25, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    445
  5. =?Utf-8?B?QXNrLk5FVA==?=

    Virus Scanning to be uploaded files

    =?Utf-8?B?QXNrLk5FVA==?=, Dec 12, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    415
    Joerg Jooss
    Dec 12, 2005
Loading...

Share This Page