Need to pull matched string plus a few additional bytes



I am working on my very first program and have run into a bit of a
roadblock. I am trying to print a report of users who show up in an IIS
Log file. The good news is that the format of the userid is
WINDOWSDOMAIN\USERID. The bad news is that it is not always at the same
place in the IIS Log file due to some variable length fields that come
before it. Its location can vary left or right by about 10 bytes.



I read the IIS Log file in one line at a time. I have gotten far enough
that I can identify the lines with WINDOWSDOMAIN on it, but am stuck
there. The code $userid = substr($logfile_in, 33, 12); gets me close
but depending on the length of the date, the time or the IP address, it
is usually off by a few bytes. A sample of the input is below to
explain what I am talking about.



2006-10-23 12:08:47 24.32.35.123 WINDOWSDOMAIN\USERID 175.128.127.43 80
GET /itd/styles/main.css

2006-10-23 12:08:47 24.32.35.123 WINDOWSDOMAIN\USERID 175.128.127.43 80
GET /itd/styles/contents.aspx

2006-10-23 12:08:47 24.32.35.123 WINDOWSDOMAIN\USERID 175.128.127.43 80
GET /itd/styles/footer.aspx



Essentially what I need to do is find the WINDOWSDOMAIN on a line, and
write to a file the matched string plus \USERID data (up to the next
space). Does anyone have any suggestions? I'm thinking there must be
some very easy way to do it since Perl is made for this sort of thing.
I remember reading about some Perl built-in capability that would take a
scalar variable and parse it into an array based on a delimiter, but I
can't remember what it is. That would probably do it for me. But if
you know of a better way, I'm all ears.



Below is the code I am using.





# My first PERL program.



open USERIDOUT, ">userid.out.txt";

open IISLOG, "<ex061023.log";



$ctr = 0;

$hit_counter = 0;

$miss_counter = 0;

$logfile_in;

$userid;



while (<IISLOG>)

{

$logfile_in = $_;

if ( ($logfile_in =~ m/WINDOWSDOMAIN/i && $logfile_in =~
m/itd/i)

)

{

print "\n** Found success\n";

$hit_counter += 1;


$userid = substr($logfile_in, 33, 12);
# This is not correct but is somewhat close

print "\n", $userid;


}

else

{

print "Did not find success\n";

$miss_counter += 1;

}

}



print "\n Hit Counter = ", $hit_counter;

print "\n Miss Counter = ", $miss_counter;

print "\n Total Records Counter = ", $hit_counter + $miss_counter;



close USERIDOUT;

close IISLOG;





*****************************************************

Phil
Confidentiality Notice:
This e-mail and any attachments may contain confidential information intended solely for the use of the addressee. If the reader of this message is not the intended recipient, any distribution, copying, or use of this e-mail or its attachments is prohibited. If you received this message in error, please notify the sender immediately by e-mail and delete this message and any copies. Thank you.


Relevant Pages

  • Re: Need to pull matched string plus a few additional bytes
    ... The good news is that the format of the userid is ... place in the IIS Log file due to some variable length fields that come ... I read the IIS Log file in one line at a time. ...
    (perl.beginners)
  • Re: leading null characters produced in script...
    ... way I do it, when a log file reaches a certain size, I have an external ... characters at the beginning of the new log, ... cp $logFile $logFile.arch ... your first program has the log file open when you ...
    (comp.os.linux.misc)
  • Re: die and write to log file
    ... > usually when opening a file, we should check whether it can be opened ... Assuming the user can access the log file: ... For example getting the userid ... Also there are cpan things that will do the same ...
    (comp.lang.perl.misc)