Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by sroux (Acolyte)
on Sep 01, 2013 at 21:05 UTC ( #1051863=perlquestion: print w/ replies, xml ) Need Help??
sroux has asked for the wisdom of the Perl Monks concerning the following question:

Dear guruz, Is there any way of doing a one liner grep and displaying the matching lines but also the next line? > This would interests me (unix version with -A option) : $ grep -A1 error file test_log.txt I found this basic one liner grep : perl -nle "print if /error/" test_log.txt Many thanks for lightening my path.

Comment on perl one line for advanced grep (current line and next one)
Re: perl one line for advanced grep (current line and next one)
by atcroft (Monsignor) on Sep 01, 2013 at 21:34 UTC

    This may not be the best or most efficient way to do it, but it does appear to work (written on multiple lines for readability):

    Code:

    Example output:

    I started with a single if (m/.../ or $flag) statement, but in testing found that it missed the following line if the search value appeared in consecutive lines. If you want more following context, change the mod value to N+2, where N is the number of lines you want to see including the matching one (i.e., if you want 1 extra line, set to 3, 2 extra to 4, etc.).

    Hope that helps.

Re: perl one line for advanced grep (current line and next one)
by rjt (Deacon) on Sep 01, 2013 at 21:44 UTC

    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.
      I'll golf it readable (untested):
      perl -ne ' print, $aft++, next if /PAT/; $aft or next; print "$_---\n"; $aft = 0;'
Re: perl one line for advanced grep (current line and next one)
by kschwab (Priest) on Sep 02, 2013 at 04:43 UTC
    perl -ne 'print $_.=<> if /string/' testfile

      Nice, but it misses the line following a second consecutive line matching the pattern

      # eg this works as expected test1 string test2 test3 # this one fails to print test2 test1 string string test2 test3

      Cheers,
      R.

      Pereant, qui ante nos nostra dixerunt!
        It also hangs if the last line matches. So much for working off the top of my head.
Re: perl one line for advanced grep (current line and next one)
by hdb (Prior) on Sep 02, 2013 at 08:29 UTC

    Here is my humble variation:

    perl -ne "print if /error/ || $last=~/error/; $last=$_;" test_log.txt
Re: perl one line for advanced grep (current line and next one)
by roboticus (Canon) on Sep 02, 2013 at 14:56 UTC

    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.

      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: perlquestion [id://1051863]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2014-10-23 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls