Jerry Rocteur wrote:
> 
> Hi,

Hello,

> Anyone know how to write a grep -C in Perl ?

Yes, someone definitely does:
http://www.perl.com/language/ppt/src/grep/index.html


> You know, I want to search for a string/regexp in a file and I want to
> print the line before and the line after the search string.
> 
> I've tried a couple of ways and it takes a really long time, I load the
> file in memory, seach for the string, extract the line number of the
> string $. subtract one and add one and print the three lines but this
> can take a very long time.. I don't have grep -C on the system I want
> to do this.
> 
> I've looked at perldoc and the list archives and can't fing any help..


my $before = 1;
my $after  = 1;
my @buffer;

while ( my $line = <FILE> ) {

    push @buffer, $line;

    shift @buffer if $#buffer > $before + $after;

    next if $#buffer < $before;

    if ( $buffer[ $before ] =~ /found matching regex/ ) {

        print @buffer;

        }
    }

__END__



John
-- 
use Perl;
program
fulfillment

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

Reply via email to