Basically I have a semi large file (5 megs+) that contains output sperated by new lines. What I want to do is get the last $n lines that match a certain criteria.
My immediate thought was to use tail, in the spirit of not reinventing the wheel. But (as far as I can tell) tail only has an option to specify the number of lines from the very bottom. Thus if the $n lines weren't contained in the first sample (default 10 lines or what not) then I would have to specify a larger number of lines from the bottom, tail -n 20 and so on until I find the $n lines I want. While this is workable I suppose, probably in the form of:
my @lines; while(@lines < $n) { @lines = grep/criteria/,split/\n/,`tail -n $i file.foo`; $i+=$n; }
But the idea of having to reparse the same lines over and over (bottom 10, then bottom 20) and so on repeatedly kind of rankles. Perhaps it's not an overwhelming problem, but it bothers me =]. And of course I would have to make constant calls to tail, perhaps 5 or more calls from a single invocation. This doesn't seem to bode well for efficiency..

My second thought was that I could do something along the lines of:
my @file = <FILEHANDLE>; for(@file) { push @lines,$_ if /criteria/; last if @lines>$n; }
But of course slurping a semi massive file into memory is going to incur even worse penalties then using tail.

Last but and probably least, I could do something along the lines of tail -f and have a deamon that constantly watches the file in question and keeps some sort of database of the last 10 lines that match my criteria. This might be the most efficient of the three, but it seems vastly more complicated, in that I have to maintain the deamon, make sure it's running and configured properly, etc. Any thoughts?

In reply to last $n lines that match a criteria by BUU

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.