In Perl I am trying to read a log file and will print only the lines that have a timestamp between two specific times. The time format is hh:mm:ss and this is always the third value on each log. For example, I would be searching for lines that would fall between 12:52:33 to 12:59:33
I am new to Perl and have no idea which route to take to even begin to program this. I am pretty sure this would use some type of regex, but for the life of me I cannot even begin to fathom what that would be. Could someone please assist me with this.
Also, to make this more difficult I have to do this with the core Perl modules because my company will not allow me to use any other modules until they have been tested and verified there will be no ill effects on any of the systems the script may interact with.
In pseudocode, you’d do something like this:
This may be too advanced for your needs, but the flip-flop operator
..immediately comes to mind as something that would be useful here.For reading in a file from stdin, this is the conventional pattern:
Parsing a line into fields can be done easily with
split(see perldoc -f split). You will probably need to split the line by tabs or spaces, depending on the format.Once you’ve got the particular field (containing the timestamp), you can examine it using a customized regexp. Read about those at perldoc perlre.
Here’s something which might get you closer: