Re: GNU grep -C in Perl

2002-12-10 Thread John W. Krahn
"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/ ) { > >

Re: GNU grep -C in Perl

2002-12-10 Thread John W. Krahn
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

Re: GNU grep -C in Perl

2002-12-10 Thread Jason Tiller
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

RE: GNU grep -C in Perl

2002-12-10 Thread Hanson, Rob
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

Re: GNU grep -C in Perl

2002-12-10 Thread Jerry Rocteur
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"; }

Re: GNU grep -C in Perl

2002-12-10 Thread Larry Coffin
>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) {

GNU grep -C in Perl

2002-12-10 Thread Jerry Rocteur
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