Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: perl one line for advanced grep (current line and next one)

by rjt (Deacon)
on Sep 01, 2013 at 21:44 UTC ( #1051866=note: print w/ replies, xml ) Need Help??


in reply to perl one line for advanced grep (current line and next one)

How long can the line be?

There's no perlrun switch that makes this easy, but regular old IF-ELSIF and a state variable will get the job done.

perl -ne 'if (/PAT/) { print; $aft = 1 } elsif ($aft) { print "$_---\n"; $aft = 0; }' FILENAME

No doubt this could be golf'd into submission, but left reasonably verbose here for clarity.

The bigger question (to me) is, "why?" My system grep implements Perl regular expressions, plus there have been multiple Perl grep implementations such as grepp -- Perl version of grep. There is a point at which the convenience of a one-liner can't make up for the power of a purpose-built utility. Maybe this isn't that point, but it'd be getting awfully close, for me.

use strict; use warnings; omitted for brevity.


Comment on Re: perl one line for advanced grep (current line and next one)
Select or Download Code
Replies are listed 'Best First'.
Re^2: perl one line for advanced grep (current line and next one)
by Anonymous Monk on Sep 02, 2013 at 16:58 UTC
    I'll golf it readable (untested):
    perl -ne ' print, $aft++, next if /PAT/; $aft or next; print "$_---\n"; $aft = 0;'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1051866]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2015-07-31 07:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (275 votes), past polls