Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting messages from log file 1

Status
Not open for further replies.

behunter

Technical User
Dec 15, 2000
2
US
I would like to extract all messages that pertain to a particular IP address from a logfile, using the IP address as an argument. This seems like it should be easy to do, but as a newbie to perl I am stumped. The logfile looks like this...

Sent:
Host=199.199.199.199
Filename=xxxxxxxx.xxxxxx
Date=mm.dd.yyyy:hh.mm.ss
0 or more lines of comments

Received:
Host=33.33.33.33
Filename=xxxxxxxx.xxxxxx
Date=mm.dd.yyyy:hh.mm.ss
0 or more lines of comments

Received:
Host=199.199.199.199
Filename=xxxxxxxx.xxxxxx
Date=mm.dd.yyyy:hh.mm.ss
0 or more lines of comments

etc....

Thanks in advance,
behunter
 
The following code will open the logfile, read until it finds the proper IP address, then print all lines until it finds a blank line, then repeat the process.
Code:
$filename = "your logfile path/name";
$ip = "the desired ip address";
open(LOGFILE, $filename);
while ( <LOGFILE> ) {
   next unless /$ip/;
   while ( <LOGFILE> && !/\A\s*\Z/; ) {
      print $_;
   }
}
close LOGFILE;
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top