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

Re^3: searching across a file then parsing

by oko1 (Deacon)
on Feb 17, 2012 at 05:47 UTC ( #954406=note: print w/replies, xml ) Need Help??

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


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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://954406]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2017-02-26 03:22 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (371 votes). Check out past polls.