in reply to Re: Extract lines between two values from file
in thread Extract lines between two values from file

This will still present a problem if there don't happen to be any events between 09:05:00 and 09:05:01, though. As choroba said, $start_point and $end_point have to appear literally at least once each in the file for pattern matching to work in this way. If $end_point is "09:05:00", it's not going to know to stop between lines having times of "09:04:59" and "09:05:01".

To make that work, you'll need to loop through the file, extracting the date/time and comparing it to $start_point until a line's date is greater than that value, then start processing lines and doing the same with $end_point until you hit a line with a date greater than $end_point, and then stop.

That also assumes that your log entries are always in date-order. That's usually true, but it's at least theoretically possible that different processes could write to the same log file slightly out of order, in which case you'd want to just check all times for being greater than $start_point and less than $end_point, and not worry about starting or stopping at certain lines.

  • Comment on Re^2: Extract lines between two values from file

Replies are listed 'Best First'.
Re^3: Extract lines between two values from file
by plexy (Initiate) on Nov 08, 2011 at 14:42 UTC

    Hi aaron_baugher,

    Yes, after reading up on flip-flop I became aware of the fact that both values must exist. :-) It's 99,9999% guaranteed that these logs has something written in them each and every second.

    A good enough "solution" to make it stop (at least approximately around) the time it should, if the $end_point second is non-existent, might be to just set $end_point to "09:05". Worst case scenario, the same SMS-message(s) might be counted within two different time periods, but like I said it's almost sure that something is written in the logs every second.

    As you probably understood I'm not a perl-programmer, and I can barely call myself Google-perl'er. Therefor I'm not sure if I want to try other methods when this is (probably/hopefully) going to work most of the time.

    Of course, like you say, it's possible that the timestamps are out of order, and that the script should be more robust. I guess I'll have to google a bit after all... :-)

    Thanks a lot for your input!