Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Can I regex & grep to select contiguous lines?

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


Description:

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (11)
    As of 2014-08-22 19:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (163 votes), past polls