Solved

Extracting data from log file w/ pattern recognition

Posted on 1997-06-10
1
375 Views
Last Modified: 2011-09-20
What is the best way to assign variables to data in a log file?  

Example:
       If I wanted to extract the IP address for the latest log-on of a user from the pm.log file in /var/log to use to compare it to another IP address, how would I instruct perl to find the IP address for that user at that time in that log file?

I've tried pattern recognition, but can't get it to work properly  What would be the syntax if I used patern recog?.


0
Comment
Question by:delaney
1 Comment
 

Accepted Solution

by:
cymbal earned 100 total points
ID: 1204242
I think pattern matching is nearly always going to be easiest, unless the information in your log file is delimited in some way in which case you can use split to split each line on a delimiter and assign to variables.

eg. If your log contains:
User bill logged on at 3 am, his IP address is  :321.321.321.321
User bob logged on at 4 am, his IP address is :123.432.123.321

Then you can split on the colon to get the IP address like this...
...
($boringstuff, $IPaddress) = split /':'/,$linefromfile;
...

If the IP address is buried in normal text with no obvious delimiters, then you could try this to match an IP address from a file using pattern recognition:

$linefromfile =~ /(\d+\.\d+\.\d+\.\d+)/;
# Match: digits dot digits dot digits dot digits
$IPaddress = $1; # Store the IP address

Hope this helps

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question