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

Hi there, I have a log file which looks like this:-
Software Package: "MS08-056_KB956464-Officexp_W2K_ENU.1.0.1.0" Operation: install Mode: not-transactional,not-undoable | force | notify_inv +entory Time: 2008-10-28 08:23:52 Log File: catrsts010bns3a:e:\logs\MS08-056_KB956464-Officexp_ +W2K_ENU.1.0.1.0.log ================= DISSE0074I Operation successfully submitted. Distribution ID is 184743 +9935.229087. ================= Software Package: "MS08-056_KB956464-Officexp_W2K_ENU.1.0.1.0" Operation: install Mode: not-transactional,not-undoable | force | notify_inv +entory Time: 2008-10-28 08:38:42 Log File: catrsts010bns3a:e:\logs\MS08-056_KB956464-Officexp_ +W2K_ENU.1.0.1.0.log ================= TEST: DISSE0155I Distribution ID: `1847439935.229087' DISSE0029I Current software package status is 'IC--E'. DISSE0005E Operation unsuccessful. DISSE0442I Execution of user program 'during_install - C:\Staging\MS08 +-056_W2K_ENU.1.0.1.0\setup.exe (/s)' completed with result: 'success' +. DISSE0198I User program exit code: 0 DISSE0111E Unable to remove the path C:\Staging\MS08-056_W2K_ENU.1.0.1 +.0. Errno: 2089877656. =================
I want to split it by ================= and then if the 4th line of a block is DISSE0029I Current software package status is 'IC--E' then add the first line of the block in an array, if not ignore that block. Your help will be greatly appreciated. This output of the log file will be:
TEST:

Replies are listed 'Best First'.
Re: File parsing
by GrandFather (Saint) on Nov 08, 2008 at 03:32 UTC

    There are a couple of tricks that you can use to make this a fairly simple problem. First is you can set the special variable $/ to '=================' to read a block at a time:

    open my $inFile, '<', $filename or die "Failed to open $filename: $!\n +"; local $/ = '================='; while (defined (my $record = <$inFile>)) { chomp $record; ... }

    You can then split the record using "\n" to get a list of lines:

    my @lines = split "\n", $record;

    Perl reduces RSI - it saves typing