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!
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/;
}
| [reply] [d/l] |
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>
| [reply] [d/l] |
Re: Text match
by lima1 (Curate) on Oct 14, 2009 at 15:18 UTC
|
| [reply] |
|