http://www.perlmonks.org?node_id=954406


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
    oko1, Thanks for taking the time to comment. This is actually exactly what I'm looking for. I apologize for possibly mis-leading the readers with my "large file"...that was relative and the file isn't really all that large at all...only a few Kbs...but, does vary. I've updated the post to give a better idea what I'm attempting. I think this really helps. What I'm doing is based on an operator input (something like "OPS" and "REV") parse the file unil I've found the specific information I'm looking for. then, I want to grab everything relevant. The issue is that the line I've been looking at "1. OPS 7554 REV 225888" is below that actual line that I need to pull in. But, I think this will get me much farther than I am now. Much appreciated.