Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by roboticus (Chancellor)
on Sep 02, 2013 at 14:56 UTC ( #1051946=note: print w/ replies, xml ) Need Help??


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

sroux:

I normally don't do one-liners, so I thought I'd play with it a bit. Here's my shot at it:

perl -ne '$fl=$.+2 if /regex/; print if $fl>$.' testfile

Update: here's another:

perl -ne 'print $t,$_ if $t=~/regex/; $t=$_' testfile

Update: ...but the second one is broken, as kschwab mentions...

...roboticus

When your only tool is a hammer, all problems look like your thumb.


Comment on Re: perl one line for advanced grep (current line and next one)
Select or Download Code
Re^2: perl one line for advanced grep (current line and next one)
by kschwab (Priest) on Sep 02, 2013 at 17:32 UTC
    The latter misses the last line of the file if it has matching text...I like the first one though.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2015-07-01 23:52 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 (25 votes), past polls