Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: How to parse a logfile to a HTML table format

by HyperZonk (Friar)
on Jul 22, 2001 at 02:28 UTC ( [id://98708]=note: print w/replies, xml ) Need Help??


in reply to How to parse a logfile to a HTML table format

Update: I note that some people are recommending substr solutions. If your log is like mine, it isn't actually fixed width fields (though it may appear to be at first, if all of the IP addresses have the same width). There is, of course, a way to work with that (using index), but that probably is less efficient. Personally, my favorite answer from below is the split answer, which is probably better than my regex solution.

I'm going to be very general in my answer, to hopefully increase the usefulness of the answer (and salve my conscience about being a homework-troll).

Say you've got line-length records of the form:
xxx yyy zzz aaaaa bbb cccccccccccc...
Say you want x, y, and z in one field, and the rest of the stuff in separate fields. You can populate an array (@infile) with the lines from the log file, then do the following:
foreach (@infile) { /(\S+\s+\S+\s+\S+)\s+(\S+)\s+(\S+)\s+(.*)/; my @fields = ($1,$2,$3,$4); push @output, \@fields; }
This will create a list of lists that you can output as you need. @output is a list of references to anonymous lists populated with your desire output fields. \s parses whitespace characters, \S parses non-whitespace character. You simply arrange your parentheses to capture however many of the space delimited items you need per field.

Note: this code could be optimized for shortness, but I have tried to keep what is happening relatively obvious by explicitly doing things in several steps.

Replies are listed 'Best First'.
Re: Re: How to parse a logfile to a HTML table format
by Hofmator (Curate) on Jul 23, 2001 at 16:35 UTC

    A short remark, you suggest to

    populate an array (@infile) with the lines from the log file.
    This is absolutely not necessary in this case, it's just a waste of memory. Why not do it like this:
    open(LOG,'<','logfile') or die "Couldn't open logfile: $!"; while (<LOG>) { # do something } close LOG;

    -- Hofmator

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-28 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found