Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: upto match please, my one-lina!!

by roboticus (Chancellor)
on Jun 09, 2006 at 00:24 UTC ( #554392=note: print w/replies, xml ) Need Help??

in reply to upto match please, my one-lina!!


Howzabout this?

perl -ne 'print @t if /foo/; push @t,$_; shift @t if $#t>5' file
You can even slice @t in the print if you want to have a larger gap between the last printed item and the regular expression...

UPDATE: I golfed it down a little:

perl -ne 'print @t if /foo/; @t=(@t[1..4],$_)' file

Replies are listed 'Best First'.
Re^2: upto match please, my one-lina!!
by jwkrahn (Monsignor) on Jun 09, 2006 at 03:56 UTC
    That doesn't print the "---\n" string or the correct lines. You want something like:
    perl -ne'@t=(@t[1..4],$_);/regex/&&die@t,"---\n"' file

      I was having so much fun playing with one-liners that I forgot the parameters of the problem. I love that die trick.... I'll have to try it:

      perl -ne '@t=(@t[1..4],$_,"---\n");die@t if/foo/' file
        You are adding two lines for every one line you remove so every second line will be "---\n".

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://554392]
[robby_dobby]: marto: yeah, I'd eventually visit Glasgow some day - just to visit the home town of GHC :-)
[robby_dobby]: Corion: Oh, that's awesome! Your timing is perfect enough to see all hell break loose when you get back at work :P
[marto]: well, let me know in advance, I'll buy you a pint :)
[Corion]: robby_dobby: No, I'm returning in the second workweek of January. The main hectic parts are in the first days after the start of the new year
[robby_dobby]: marto: and, I'm not sure I'd be around here long enough for YAPC::EU
[marto]: Corion nice, what are you plans for Christmas?
[robby_dobby]: Corion: Nice, you just won't be around to deal with the mess - enjoy your vacation :-)
[robby_dobby]: marto: Sure, will do. Thanks!
[Corion]: marto: No great plans - I'll meet with my sister, my brother and my mother, but that's all :)
[Corion]: ... and also a visit to my godson+family of course :)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2017-12-15 10:41 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (431 votes). Check out past polls.