Hello,

I assume you know how to use the function split. Type "perldoc -f split" if you don't. Using split, you can get all fields and join the fields you want to produce the strings required.

Now from looking at your logs, it seems like all columns have fixed widths. The first 21 chars go to the first cell in the table. Skip one field, then grab the next 13 chars, skip one and get the rest.

To match these fields, one can use a regular expression to do the job. /^(.{21}) (.{13}) (.*)/ should match 21 chars, followed by a space, followed by 13 chars, followed by a space, then the rest. In list context, the matching operator returns a list of all things between brackets.

# example:
my $str = "Sat Jul 21 22:30 2001 144.02.26.399 www.myserver.com";
my ($date, $stuff, $servers) = $str =~ /^(.{21}) (.{13}) (.*)/;
So, to accomplish your task, just do a simple:
print "<table>";
for(@lines){
    print "<tr>";
    print "<td>$_</td>" for /^(.{21}) (.{13}) (.*)/;
    print "</tr>";
}
print "</table>";
And you should be done.

Hope this helps,,,

Aziz,,,


In reply to Re: How to parse a logfile to a HTML table format by abstracts
in thread How to parse a logfile to a HTML table format by Suwen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.