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";
}
$reml = $_;
}
That is certainly much quicker than the way I was doing it before..
I'd still like to see if there is a cleaner way though.
Also, to make it more flexible, perhaps print more than one line before and more than one line after.
TIA,
Jerry
On Tuesday, Dec 10, 2002, at 21:22 Europe/Brussels, Jerry Rocteur wrote:
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, 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..
Thanks in advance,
Jerry
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]