in reply to Re^2: searching across a file then parsing
in thread searching across a file then parsing
This is purely a guess, but I suspect that rather than a character-by-character numerical position change (like that provided by 'seek'), the OP is looking for a "line-by-line cache" - similar to something I did recently.
I'm going to assume that the file is too big to stuff into a scalar and process with something like /($start.*?$match.*?$end)/ - i.e., that it does indeed need to be read one line at a time.
#!/usr/bin/perl use common::sense; my $pat1 = "TOP_PATTERN"; my $pat2 = "SEARCH_PATTERN"; my ($flag, $cache); while (<DATA>){ if ($flag){ last if /$pat1/; print and next; } if (/$pat1/){ $cache = "" and next if $cache; $cache .= $_ and next; } if ($cache){ $cache .= $_; $flag = 1 and print $cache if /$pat2/; } } __END__ TOP_PATTERN 1 2 3 TOP_PATTERN 4 5 6 SEARCH_PATTERN 7 TOP_PATTERN 8 9
Output:
TOP_PATTERN 4 5 6 SEARCH_PATTERN 7
-- I hate storms, but calms undermine my spirits. -- Bernard Moitessier, "The Long Way"
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: searching across a file then parsing
by newperlplayer (Initiate) on Feb 20, 2012 at 01:47 UTC |
In Section
Seekers of Perl Wisdom