"John W. Krahn" wrote:
>
> my $before = 1;
> my $after = 1;
> my @buffer;
>
> while ( my $line = ) {
>
> push @buffer, $line;
>
> shift @buffer if $#buffer > $before + $after;
>
> next if $#buffer < $before;
>
> if ( $buffer[ $before ] =~ /found matching regex/ ) {
>
>
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
Hi, Jerry, :)
On Tue, 10 Dec 2002, Jerry Rocteur wrote:
> Anyone know how to write a grep -C in Perl ?
Here is a script that mimics just the '-C' feature of GNU grep. It
doesn't support the fanciness of printing filenames when multiple
input files are specified.
This may be overkill for your s
Message-
From: Jerry Rocteur [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, December 10, 2002 3:23 PM
To: [EMAIL PROTECTED]
Subject: GNU grep -C in Perl
Hi,
Anyone know how to write a grep -C in Perl ?
You know, I want to search for a string/regexp in a file and I want to
print the line before
Hi,
Reading further in the archives to this list I got an idea: (thanks to
John W. Krahn)
#!/usr/bin/perl
$pattern = shift(@ARGV);
while (<>) {
if ( /$pattern/i ){
print $reml;
print $_;
$_ = <>;
print "$_\n";
}
>Anyone know how to write a grep -C in Perl ?
Why not just store the previous line, then when you have a match,
print it, the matching line, then set a flag to print the next line as well?
Something along the lines of:
while ($line = ) {
if ($print_next) {
Hi,
Anyone know how to write a grep -C in Perl ?
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, extr