Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: get n lines before or after a pattern

by zentara (Archbishop)
on Jul 25, 2012 at 16:28 UTC ( #983697=note: print w/replies, xml ) Need Help??

in reply to get n lines before or after a pattern

Untested, but a useful approach.
#!/usr/bin/perl use strict; use warnings; my @buffer; # a queue data structure while ( <DATA> ) { if ( /I sent/ ) { print @buffer; # 3 lines before print; # the matching line print scalar(<DATA>); # 1 line following last; # all done } push @buffer, $_; shift @buffer if @buffer > 3; } __DATA__ this is the output from the command I sent to the command interperter

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://983697]
[jedikaiti]: If I had a flying home, I'd be pretty psyched. How cool would that be?
[jedikaiti]: The big question is, does it have autopilot?
[shmem]: good autonomous flyer doesn't need a pilot.
[karlgoethebier]: The One And Only Flying Home

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (12)
As of 2018-02-21 17:37 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (285 votes). Check out past polls.