Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Text match

by NorthShore44 (Novice)
on Oct 14, 2009 at 14:32 UTC ( #801097=perlquestion: print w/replies, xml ) Need Help??

NorthShore44 has asked for the wisdom of the Perl Monks concerning the following question:

Monks- I'm running a program that searches for a text match within a txt file and I'm currently able to pull the text after the match, but how can I adjust my code to also pull text before the match? I'm currently able to pull the twenty lines of text after the match, but how can I pull, say twenty lines of text before the match as well? Thanks!

Replies are listed 'Best First'.
Re: Text match
by kennethk (Abbot) on Oct 14, 2009 at 14:40 UTC
    It's certainly be helpful if you posted some code - see How do I post a question effectively?. Do you have all your lines in an array? Do you have the whole file in a string? The solution depends strongly on these questions. Assuming you want to do it streaming, perhaps you mean something like:

    while (defined (my $line = <$file>)) { push @cache, $line; shift @cache if @cache > 20; print join "\n", @cache if $line =~ /test/; }
Re: Text match
by rovf (Priest) on Oct 14, 2009 at 14:34 UTC

    You could, for instance, always keep the most recent 20 lines of text.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: Text match
by lima1 (Curate) on Oct 14, 2009 at 15:18 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://801097]
Approved by rovf
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2021-12-07 00:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (33 votes). Check out past polls.

    Notices?