Looping through a log file

A

AJ Hartley

My project is to parse a text log file created by a propietary
publishing application and stuff the important information into a
database. The actual parsing of the information for a single event
using regular expressions was a slam dunk, though each line requires
it's own expression. But I'm needing some advice with a strategy for
looping through the file.

I'm running this process every mintute or so, no more than 10 events
can stack up in the file during that period.

Each "event" in the log file may contain four or five lines of
information. That's the parsing process mentioned above. Each event is
concluded with a dashed line. If the next line contains nothing, we can
exit. Otherwise the loop continues. Example:

Event 1 title
Event 1 user info
Event 1 time stamp info
Event 1 status info
Event 1 file path info
----------------------------------------
Event 2 title
Event 2 user info
Event 2 time stamp info
Event 2 status info
----------------------------------------
and so on...

My challenge is to figure out how run the parsing on each chunk and
then continue on to the next or exit as necessary based on the dashed
line.

Thanks for any advice you can offer...
 
A

Arndt Jonasson

AJ Hartley said:
My project is to parse a text log file created by a propietary
publishing application and stuff the important information into a
database. The actual parsing of the information for a single event
using regular expressions was a slam dunk, though each line requires
it's own expression. But I'm needing some advice with a strategy for
looping through the file.

I'm running this process every mintute or so, no more than 10 events
can stack up in the file during that period.

Each "event" in the log file may contain four or five lines of
information. That's the parsing process mentioned above. Each event is
concluded with a dashed line. If the next line contains nothing, we can
exit. Otherwise the loop continues. Example:

Event 1 title
Event 1 user info
Event 1 time stamp info
Event 1 status info
Event 1 file path info
----------------------------------------
Event 2 title
Event 2 user info
Event 2 time stamp info
Event 2 status info
----------------------------------------
and so on...

My challenge is to figure out how run the parsing on each chunk and
then continue on to the next or exit as necessary based on the dashed
line.

Is there a need to worry about log entries being written while we are
reading, so truncated entries may appear?
 
A

AJ Hartley

I have a utility that checks for file stability before the file is
opened, read and moved to another location. The publishing application
then starts a fresh log file. So my belief is that truncated entries
will not appear.
 
T

Tad McClellan

AJ Hartley said:
Each event is
concluded with a dashed line.

Event 1 title
Event 1 user info
Event 1 time stamp info
Event 1 status info
Event 1 file path info
----------------------------------------
Event 2 title
Event 2 user info
Event 2 time stamp info
Event 2 status info
----------------------------------------
and so on...

My challenge is to figure out how run the parsing on each chunk and
then continue on to the next or exit as necessary based on the dashed
line.


local $/ = "----------------------------------------\n";
while ( my $record = <FILE> ) {
# "parse" the record
}
 
A

AJ Hartley

Thanks for your reply. It appears to be just the push the I needed.
I'll begin hammering this out this evening!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top