in reply to Re: push to an array lines matching a pattern, and the lines before and after the matching
in thread push to an array lines matching a pattern, and the lines before and after the matching

Technically, I wouldn't be surprised if using seek to look behind in a sliding window is more efficient than buffering the last line.¹

But it's hard to believe that there isn't already an full featured unix-grep emulation in pure perl, and we are reinventing the wheel.

Have to admit, I'm too lazy to search CPAN now ...

Cheers Rolf

UPDATE: ¹) and a regex even more.

  • Comment on Re^2: push to an array lines matching a pattern, and the lines before and after the matching

Replies are listed 'Best First'.
Re^3: push to an array lines matching a pattern, and the lines before and after the matching
by JavaFan (Canon) on Mar 03, 2012 at 00:41 UTC
    But it's hard to believe that there isn't already an full featured unix-grep emulation in pure perl, and we are reinventing the wheel.
    It's called ack. And it is a reinvention of the wheel. acks usefulness doesn't come from the fact it's implemented in Perl, but because it has some features that grep doesn't have, and has some better defaults.

    In the OP's case, he's better of with calling grep, then with ack. (grep is expected to be faster (as it's in C, not in Perl))

      Thanks! I remember now...

      Great name BTW, easy to guess! (sarcasm)

      At least man -k grep lists it.

      Cheers Rolf

      thanks for the ack suggestion, but it is slower than the "pure perl" implementation when using anchors or \b,\s etc
      sub sub6 { #perl ack my $p = $_[0]; #pattern my $mR = $_[1]; #more rows my @values; my $time = [gettimeofday]; my @valori = qx (ack -C $mR "$p" textMatchInAfile.txt) or die "system @values` failed: $?"; say 'number of values found with ack' . $#valori; say 'time sub6 ack' . tv_interval($time); }