You might find it more clear to use regular expressions to parse the input. Properly constructed, they can be more forgiving of input format variations.

Your problem could be done in a single regex, but I'll break it down here for ease of understanding.

# grab the first two non-whitespace items, separated by whitespace. my ($date, $time) = $s =~ /(\S+)\s+(\S+)/; # grab the last two portions of the disk name, # which is immediately followed by the first ^ in the file. my ($disk) = $s =~ /\\(\w+\\\w+)\^/; # grab the user name, which is the string before the last ^, # which in turn is followed by some white space and the end of the str +ing. # You can take out the \s+ if the space between the ^ and end of strin +g was an artifact of your post. my ($user) = $s =~ /(\w+)\^\s+$/;
Note that in this example, it doesn't matter if the number of elements in the disk path changes - it will always grab the last two. In your example, a change in the path format would break both $disk and $user.

The same thing is true of the search for the user name. As long as it is the string before a ^ at the end of the line, it will always parse correctly with the regex, even if the format of what comes before it changes.

Regular expressions take some work to get used to, but that effort is well rewarded.


In reply to Re: parse a log file by mikeB
in thread parse a log file by Anonymous Monk

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.