Solved

Extracting data from log file w/ pattern recognition

Posted on 1997-06-10
1
354 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now