On Nov 18, Andrea Holstein said:

>> Iam a beginner in perl.I have one question,
>> Iam trying to write one prog,in which i have to search for one word in a
>> file,
>> If I found that word,print next 4 lines.
>> PLs help me,how to write code.
>
>open FILE, "<filename";
>while (<FILE>) {
>       print(<FILE>,<FILE>,<FILE>,<FILE>), last if /your_word/;
>}
>close FILE;

This wasn't tested.  print() is a list operator, so it's calling <FILE> in
list context, which will return ALL the lines.

  while (<FILE>) {
    if (/pattern/) {
      my $loc = tell FILE;
      for (1..4) { print scalar <FILE> }  # print 4 lines
      seek FILE, $loc, 0;
    }
  }

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to