Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Text match

by NorthShore44 (Novice)
on Oct 14, 2009 at 14:32 UTC ( [id://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?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-19 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found