[Israel.pm] context grep in Perl
Yossi Itzkovich
Yossi.Itzkovich at ecitele.com
Mon Nov 28 04:57:34 PST 2011
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: perl-bounces at perl.org.il [mailto:perl-bounces at perl.org.il] 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
______________________________________________________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.perl.org.il/pipermail/perl/attachments/20111128/fa830077/attachment-0001.htm
More information about the Perl
mailing list