Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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


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!
  • 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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [marioroy]: Perl is so much fun.
    [Discipulus]: Tanktalus i recently started a Meditation about marioroy's MCE suit of modules, but is even better to see a practical question answered than responses to my meditation
    [Discipulus]: good night mario!
    [Tanktalus]: yeah, I saw you post about MCE - the concept looks really really cool. I wonder how well it plays with Coro :)
    [Lady_Aleena]: Hi guys. I asked this earlier but got no answer. Why is this dying at -exec: my @music_times = qx(find ~/Music/Albums/ -type f -iname '*.mp3' -exec mp3info -p "%S\n" {} \;);
    [Lady_Aleena]: The error find: missing argument to `-exec'
    [Discipulus]: Tanktalus we will be glad to have you here more frequently, tho ;=)
    [marioroy]: Lady_Aleena oh I thought you found the error by adding -exec mp3info
    [Tanktalus]: Discipulus: I should be working on an interview test right now. Well, I kind of am, but keep getting myself distracted :)

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (5)
    As of 2017-04-23 19:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      I'm a fool:











      Results (432 votes). Check out past polls.