plexy has asked for the wisdom of the Perl Monks concerning the following question:
Hi guys
We have a service which we provide to customers where I work, and this service generates an SMS directly to customers when "something bad happens". Everytime a SMS is fired away it gets logged in a logfile (containing a bunch of other things as well). What I'm trying to achieve is to extract and count sent messages within a certain period of time from the logfile.
* Logs are stored in files pr hour, and the filename has the format <filename>.log.<yyyy>-<mm>-<d d>-<hh>, for example <filename>.log.2011-11-08-09
.
* Every line in the log starts with a timestamp, for example "2011-11-09 09:00:00,000"
* Each lines start with a timestamp, for example "2011-11-08 09:00:00,000"
* Log entries which contains sent messages has the "key"(...) "sendSMS", and there's 3 different contents in the messages which I wish to count separetly:
1) 2011-11-08 09:00:03,473 INFO <<SMAPISender>> sendSMS: sender = <name>, recipient[0] = <number>, message = SMS for Sub-service 1
2) 2011-11-08 09:03:11,681 INFO <<SMAPISender>> sendSMS: sender = <name>, recipient[0] = <number>, message = SMS for sub-service 2
3) 2011-11-08 09:18:55,193 INFO <<SMAPISender>> Error sending SMS
Now, the script is run from cron every five minutes. The script starts with quite a lot of time-checking to determine which period to look between. When the start- and endpoint is defined, it's supposed to find the SMS' between these values. Example: Script runs at 09:05:00. $start_point is set to "2011-11-08 09:00:00", $end_point is set to "2011-11-08 09:04:59,999"
Here's where my problem begins (and hopefully ends). I have absolutely no idea how to make perl extract the lines between $start_point and $end_point. Here's the code for two approaches I've tried (based on google-results, but without understanding the approach it self):
Approach #1:open(FH, $filehandle); while (<FH>) { <b>if(/$start_point/../$end_point/) {</b> $line = $_; chomp $line; if ($line =~ m/SMS for Sub-service 1/) { $httpSMS++; } if($line =~ m/SMS for sub-service 2/) { $sipSMS++; } if($line =~ m/Error sending SMS/) { $errorSMS++; } } } close(FH);
Any ideas how to solve this?open(FH, $filehandle); while (<FH>) { $line = $_; chomp $line; <b>if($line =~ m/$start_point(.*)$end_point$/s) {</b> if($line =~ m/SMS for Sub-service 1/) { $httpSMS++; } if($line =~ m/SMS for Sub-service 2/) { $sipSMS++; } if($line =~ m/Error sending SMS/) { $errorSMS++; } } } close(FH);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Extract lines between two values from file
by choroba (Cardinal) on Nov 08, 2011 at 13:14 UTC | |
|
Re: Extract lines between two values from file
by ww (Archbishop) on Nov 08, 2011 at 13:43 UTC | |
by plexy (Initiate) on Nov 08, 2011 at 14:11 UTC | |
by ww (Archbishop) on Nov 08, 2011 at 14:52 UTC | |
by plexy (Initiate) on Nov 08, 2011 at 15:28 UTC | |
|
Re: Extract lines between two values from file
by plexy (Initiate) on Nov 08, 2011 at 13:34 UTC | |
by aaron_baugher (Curate) on Nov 08, 2011 at 14:03 UTC | |
by plexy (Initiate) on Nov 08, 2011 at 14:42 UTC |