Solved

Extracting data from log file w/ pattern recognition

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reading fields from the text file. 4 96
problem with using the glob function 1 69
Migrate OTRS to newest version. 1 326
Awk Question 2 127
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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