Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

File parsing

by green_lakers (Novice)
on Nov 08, 2008 at 03:21 UTC ( [id://722340]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://722340]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-25 12:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found