Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi,

If your logfile has it's data with fixed "width", then using unpack function can really come in handy! And you really wouldn't border on perl version you are using. see this:

use warnings; use strict; my $str = 'May 2 04:06:15 lon.mail.net exim[17905]: 2012-07-03 07:06:15 1SPPtO- +0004en-PS <= me@ours.co.uk H=smtpout.mail.com [22.5.10.4] I=[6.5.14.4 +]:25 P=esmtp S=13333 id=6aeca3b79b8892d6105dab131c76f066@localhost.lo +caldomain T="Half price offer"'; my ( $e_mail, $ip ) = unpack "x82A13x21A9", $str; print "EMAIL: ", $e_mail, "\nIP: ", $ip, $/; # OR while (<DATA>) { my ( $e_mail, $ip ) = unpack "x82A13x21A9", $_; print "EMAIL: ", $e_mail, "\nIP: ", $ip, $/; } __DATA__ May 2 04:06:15 lon.mail.net exim[17905]: 2012-07-03 07:06:15 1SPPtO-0 +004en-PS <= me@ours.co.uk H=smtpout.mail.com [22.5.10.4] I=[6.5.14.4] +:25 P=esmtp S=13333 id=6aeca3b79b8892d6105dab131c76f066@localhost.loc +aldomain T="Half price offer"
OUTPUT
EMAIL: me@ours.co.uk
IP: 22.5.10.4

Check perldoc perlpacktut for more info.

UPDATE: Oops! my bad I missed that but was pointed out by Kenosis though, Please Note however, if the length of the field to be gotten varies, then unpack function will NOT also work.
However, I had mentioned perviously that the logfiles data MUST have a FIXED WIDTH.


In reply to Re: String Matching by 2teez
in thread String Matching by stevbutt

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2024-04-16 08:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found