Hi Craig,

You need to use a counter.
my $count =0;
<in loop>
$count++;

You will need to also check the counter on each loop
iteration using 'if' 'else' and 'last' to exit the
loop..

my $count=0;

if ($count > 4)
{
last
}
else
{
 $count++;
}

there's several ways to do this

Here is one.. Incidentally, unless you are only
expecting small files, it is inadvisable to read the
file into an array. This eats memory needlessly. And
if a huge file was used, you could kill a machine...

Anyway here's the answer. 

> #!/usr/bin/perl -w
#####you should use strict EVERYWHERE!
use strict;
> 
> open (INFILE,"filemon2005012970.out") || die("Cannot
> open file: $!");
> 
my $count=0;
my $got_line_flag = 0;

#read the next line from INFILE and sore it in a local
variable called $line

while (my $line=<INFILE>)
{
  chomp ($line);
  #Check if we've found a matching line yet
  ###if ($got_line_flag) would do, but you should
understand this means is defined and has a value other
than 0.

  if ($got_line_flag ==1)
  {
    if ($count > 4)
    {
      #this exits a foreach and a while loop...
      last;
    }
    else
    {
      print STDOUT "$line\n";
      $count++;
    }
  }
  else
  {
    if ($line =~ /\/dev\/hdisk18  description:/)
    {
      $got_line_flag = 1;
    }
  }
}

Kind Regards

Marty
p.s. respectfully meant, but I hope I haven't just
done your homework!

p.p.s I'm sure you could possibly do this with Grep
and Splice on the Array you created, provided the file
is small.. 





> foreach (@LINES){
>   chomp;
>     if (/\/dev\/hdisk18  description:/){
>      print "$_\n";
> }
> }


 --- Craig Sharp <[EMAIL PROTECTED]> wrote: 
> Hi all,
> 
> I know this is simple but I have having a brain lock
> today.
> 
> I have a file in which I need to find a particular
> line.  Once I find the line, I need to print the
> next 5 lines and quit.
> 
> The following gets me to the line but I am stuck
> from there.
> 
> #!/usr/bin/perl -w
> 
> open (INFILE,"filemon2005012970.out") || die("Cannot
> open file: $!");
> 
> @LINES = <INFILE>;
> 
> foreach (@LINES){
>   chomp;
>     if (/\/dev\/hdisk18  description:/){
>      print "$_\n";
> }
> }
> 
> Thanks,
> 
> Craig
> 
> 
> _______________________________________________
> Perl-Unix-Users mailing list
> Perl-Unix-Users@listserv.ActiveState.com
> To unsubscribe:
> http://listserv.ActiveState.com/mailman/mysubs
>  


        
        
                
___________________________________________________________ 
ALL-NEW Yahoo! Messenger - all new features - even more fun! 
http://uk.messenger.yahoo.com
_______________________________________________
Perl-Unix-Users mailing list
Perl-Unix-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to