Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Can I regex & grep to select contiguous lines?

by dpenny (Initiate)
on Aug 24, 2002 at 01:29 UTC ( [id://192508]=perlquestion: print w/replies, xml ) Need Help??

dpenny has asked for the wisdom of the Perl Monks concerning the following question:

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!

Originally posted as a Categorized Question.

  • Comment on Can I regex & grep to select contiguous lines?

Replies are listed 'Best First'.
Re: Can I regex & grep to select contiguous lines?
by sauoq (Abbot) on Aug 24, 2002 at 20:59 UTC

    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.

      once that digit mod 5 is the same as your starting digit, you are done
      That assumes there is at least one entry for each minute... What if the log looked like:
      2:00:00 all is well 2:01:00 all is well 2:02:00 all is well 2:03:00 all is well 2:04:00 oops, reboot 2:21:00 all is well again
      I think looking at more than just that one digit is wise.

      -Blake

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://192508]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-23 20:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found