in reply to Extract lines between two values from file

Hi choroba,

Thanks for you reply.

You're absolutely right, the value of $end_point does not exist. I read up on flip-flop operator (which I of course should've done before posted), and a person describes it like this: "The first operand (the left-hand expression) is evaluated to see if it is true or false. If it is false then the operator returns false and nothing happens. If it is true, however, the operator returns true and continues to return true on subsequent calls until the second operand (the right-hand expression) returns true."

So, with other words I can change $end_point to "09:05:00". Then I'll still get everything up towards the latest entry which contains "09:04:49", all the way up to "...,999" if that exists. And as the right expressions becomes false when the next line contains "09:05:00,xxx", I wont get any values from within that minute (which is what I want)

It was a simple and easy solution, but I don't think I could've gotten out of the deep and frustrating hole I was in if it wasn't for your "thank you captain Obvious"-response, so thanks a lot. :-)

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

Replies are listed 'Best First'.
Re^2: Extract lines between two values from file
by aaron_baugher (Curate) on Nov 08, 2011 at 14:03 UTC

    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.

      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!