Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

tail and grep for Windows

by bizzach (Beadle)
on Oct 30, 2002 at 18:25 UTC ( #209185=perlcraft: print w/replies, xml ) Need Help??

   1: #!c:/perl/bin/perl -w
   2: # Windows doesn't have it
   3: # Tail and grep program 
   4: #
   5: use strict;
   6: 
   7: my $file = $ARGV[0] || printUsage();
   8: my $gs = $ARGV[1];
   9: my $filesize = 0;
  10: 
  11: open (FH,$file) || die "Unable to open $_: $!";
  12: 
  13: seek (FH,0,2);
  14: if (tell(FH) >=1500) {
  15:     seek(FH,-1500,2);
  16: } else {
  17:     seek(FH,0,0);
  18: }
  19: while (1) {
  20:     while (<FH>) {
  21: 	if ($gs) {
  22: 	    if ($_ =~ /$gs/o) {
  23: 		print $_;
  24: 	    }
  25: 	} else {
  26: 	    print $_;
  27: 	}
  28:     }
  29:     seek(FH,0,1);
  30:     sleep (1);
  31: }
  32: 
  33: sub printUsage {
  34:     die "USAGE: ptail.exe <filename> [grep string]";
  35: }
  36: 
  37: =head1 NAME
  38: 
  39: ptail.pl - A simple tail and grep program.
  40: (I was working on NT at the time and needed this)
  41: 
  42: =head1 SYNOPSIS
  43: 
  44:     ptail.pl <filename> [grep string]
  45: 
  46: =head1 DESCRIPTION
  47: 
  48:     This is a goofy little tool helpful for tailing (and grepping) logfiles.
  49: 
  50: =head1 AUTHOR
  51: 
  52: bizzach, E<lt>wbs@lookiloo.netE<gt>
  53: 
  54: =head1 SEE ALSO
  55: 
  56: L<perl>.
  57: 
  58: =cut

Replies are listed 'Best First'.
Re: tail and grep for Windows
by mojotoad (Monsignor) on Oct 30, 2002 at 21:25 UTC
    You might be interested in the Perl Power Tools project. Both tail and grep have implementations you might want to check out -- if for no other reason than comparison and hoovering some good ideas.

    Matt

    P.S. Some more info, particularly about tcgrep, in this thread.

Re: tail and grep for Windows
by Mr. Muskrat (Canon) on Oct 30, 2002 at 21:39 UTC

    Just a couple of things:
    At line 10, you are using the two argument version of the open command. I opened a DOS window and tried the following:

    copy c:\command.com c:\command2.com perl ptail.pl ">c:\command2.com"
    Now command2.com is a 0 byte file. Not good. This can be avoided by using the three argument version like:
    open (FH, "<", $file) || die "Unable to open $_: $!";

    It also has an endless loop caused by line 18. while (1) will run forever since you do not provide a way to leaving this loop.

    <nitpicking>And the program usage as displayed when run without command line arguments is only valid if you compile it.</nitpicking> =D

    Update: Removed unnecessary grep string.

Re: tail and grep for Windows
by seattlejohn (Deacon) on Oct 31, 2002 at 08:42 UTC
    Much as I love Perl, I also like not having to write code every time I miss a familiar utility. Cygwin and UnxUtils are indispensible parts of my Windows installations, precisely because I can't live without things like tail and grep.

            $perlmonks{seattlejohn} = 'John Clyman';

      GnuWin32 offers more utilities and is updated with greater frequency.
      ()-()
       \"/
        `
      

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlcraft [id://209185]
Approved by coreolyn
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2021-10-24 21:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (89 votes). Check out past polls.

    Notices?