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.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.