In a shell script or Perl, how do I open a file, find what I'm looking then
Case 1: grab that line + the next 2 lines
Case 2: grab that line + the previous 2 lines?
In Perl I'd use a rolling buffer to get previous lines.
my @buffer;
$#buffer = 2; # 3 line buffer (or whatever)
while (<>) {
push @buffer, $_;
shift @buffer;
next unless defined $buffer[0]; # No-op until buffer full
print @buffer if found_what_I_am_looking_for($_);
}
To get next lines you can either use the above approach (but test the
condition on $buffer[0]) or simply use a counter:
my $counter = 0;
while(<>) {
$counter = 3 if found_what_I_am_looking_for($_);
print if $counter-- > 0;
}
The newsgroup comp.lang.perl does not exist (see FAQ). Please do not
start threads here.