Hi

I found the perl implementation in the examples directory 
(http://examples.oreilly.com/9780596003302/)
Here it is, but I still would like to know if there is a one-liner to do this 
work:

#!/usr/bin/perl

# Usage: cgrep [-lines] pattern [files]

$context = 3;

# They might want more or less context.

if ($ARGV[0] =~ /^-(\d+)$/) {
    $context = $1;
    shift;
}

# Get the pattern and protect the delimiter.

$pat = shift;
$pat =~ s#/#\\/#g;

# First line of input will be middle of array.
# In the eval below, it will be $ary[$context].

$_ = <>;
push(@ary,$_);

# Add blank lines before, more input after first line.

for (1 .. $context) {
    unshift(@ary,'');
    $_ = <>;
    push(@ary,$_) if $_;
}

# Now use @ary as a silo, shifting and pushing.

eval <<LOOP_END;
    while (\$ary[$context]) {
        if (\$ary[$context] =~ /$pat/) {
            print "------\n" if \$seq++;
            print \@ary,"\n";
        }
        \$_ = <> if \$_;
        shift(\@ary);
        push(\@ary,\$_);
    }
LOOP_END

Yossi

From: [email protected] [mailto:[email protected]] On Behalf Of 
Yossi Itzkovich
Sent: Monday, November 28, 2011 2:29 PM
To: Perl in Israel
Subject: [Israel.pm] context grep in Perl

Hi,

In this link : http://docstore.mik.ua/orelly/unix/upt/ch27_13.htm
They mention cgrep that is implemented in Perl.

Does someone have such implementation ?
Is there a one-liner  ("real" one liner) solution ?

Regards

Yossi

This e-mail message is intended for the recipient only and contains information 
which is CONFIDENTIAL and which may be proprietary to ECI Telecom. If you have 
received this transmission in error, please inform us by e-mail, phone or fax, 
and then delete the original and all copies thereof.

______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________

______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________


This e-mail message is intended for the recipient only and contains information 
which is CONFIDENTIAL and which may be proprietary to ECI Telecom. If you have 
received this transmission in error, please inform us by e-mail, phone or fax, 
and then delete the original and all copies thereof.


______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________
_______________________________________________
Perl mailing list
[email protected]
http://mail.perl.org.il/mailman/listinfo/perl

Reply via email to