Solved

Extracting data from log file w/ pattern recognition

Posted on 1997-06-10
1
361 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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.…
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…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

914 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

19 Experts available now in Live!

Get 1:1 Help Now