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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
You said:
I need to extract the data starting with "msg=" and ending just before "Src".

If that's really all you need to do, then a regex like this would extract just that portion:

while (<INPUT>) { my ( $msg ) = ( /msg=(.*?) Src \d+ / ); # do something with $msg... }
If you need other portions of the log entry as well, there are many ways to approach the task... Something like this should be easy to maintain:
while (<INPUT>) { next unless ( s/^([\d-]+)\s+([\d:]+)\s+(\S+)\s+// ); chomp; my ( $date, $time, $ws2k ) = ( $1, $2, $3 ); # remainder of log string contains an IP addess followed by # a set of "key=value string " tuples of various sizes my ( $ip, %flds ) = split /\s+(\w+)=/; # parens keep key strings +in split output # do stuff with %flds and other vars... }
Some of the hash values that end up in %flds may need further conditioning (e.g. removing quotes), but this while loop does a thorough parse of each log entry.

UPDATE: The latter while loop will do the wrong thing if it ever turns out that one of the log field values happens to contain a substring that matches "\w+=" (the split condition). If that's a valid risk, you could assign the result from split to an array, then build the hash from the array, based on your own prior knowledge about what the key strings are supposed to be (and what order they are supposed to be in).


In reply to Re: Parsing a log file by graff
in thread Parsing a log file by TStanley

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (9)
    As of 2020-04-08 18:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      The most amusing oxymoron is:
















      Results (45 votes). Check out past polls.

      Notices?