DavidL has asked for the wisdom of the Perl Monks concerning the following question:

I am new to perl coming from VB, so please bear. I have a log file that I need to parse. Each new record has the literal “Data Record:” in the first line. I need to split the file into two files; accepted and rejected files. Furthermore, each rejected record has the literal “Server Error” in it. Each record can span multiple lines, however, each accepted record is only three lines. How would I parse this file? In essence I could read each record into an array and search for Error. Syntactically, how is this done?

Replies are listed 'Best First'.
Re: Parse/Split Log File
by tradez (Pilgrim) on Feb 13, 2002 at 22:20 UTC
    Well first you are going to have to find some delimeters. Looks like "Data Record:" is your delimeter so you should be able to first build an array based on this brash untested code
    open(MYFILE, '/dir/logfiles'); @data = MYFILE; foreach $line (@data) { if ($line =~ /^Data Record:(.*)/){ push(@push, $1); }else{ next; } }
    While writing this though I realize that this does not some up what I think it is you want done. You need to deciphers the format of delimeters between entries in the recored, ie a COMMA, and maybe a ; at the end of the record. Possibly a cut and paste of the document we are viewing could help better assist you, along with what you are trying to get out of this, Ie... propagating a database with these entries, iterating a count to a flat file or web page on how many failures as opposed to success's. Please elaborate.
    Tradez
    "Never underestimate the predicability of stupidity"
    - Bullet Tooth Tony, Snatch (2001)
Re: Parse/Split Log File
by talexb (Chancellor) on Feb 13, 2002 at 21:39 UTC
    What have you tried? Do you have any books about Perl that you can read first?

    --t. alex

    "Of course, you realize that this means war." -- Bugs Bunny.