Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Can I regex & grep to select contiguous lines?

by dpenny (Initiate)
on Aug 24, 2002 at 01:29 UTC ( #192508=categorized question: print w/replies, xml ) Need Help??
Contributed by dpenny on Aug 24, 2002 at 01:29 UTC
Q&A  > regular expressions


I have lines of a file in an array, each begins with a time stamp, HHMMSS, and are in increasing sequential order. How can I construct a grep & regex to select all lines belonging to the 1st 5 minute interval, then the 2nd, 3rd, etc. til end-of-data in array. Thanks!

Answer: Can I regex & grep to select contiguous lines?
contributed by sauoq

As your lines are in chronological order anyway, it doesn't make sense to use grep. A grep will search through the whole array but you want a set of contiguous entries and as soon as you find one outside of the right interval you know you are done searching.

You would be better off simply iterating through the lines. If seconds don't matter at all, you can just look at the last digit of your minutes and once that digit mod 5 is the same as your starting digit, you are done.

If seconds do matter, you'll have to do a little more work but the principle of iterating though the lines instead of grepping still holds.

Please (register and) log in if you wish to add an answer

  • 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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and not a whimper to be heard...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (5)
    As of 2018-06-23 10:13 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (125 votes). Check out past polls.