Solved

Extracting data from log file w/ pattern recognition

Posted on 1997-06-10
1
385 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

734 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