LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2012, 01:23 AM   #1
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Rep: Reputation: 16
perl: process strange log file


A snippet from a strange looking logfile. There may be thousands of lines like this...

Code:
2012-01-20 22:06:35 [quassi-f] LOG Start foo=a37f882dea96 op=go stat=starting file="/tmp/f823234.dat" size=23424
2012-01-20 22:06:54 [quassi] LOG Snd ll i/v/n=3221/3221/0 fio i/x/w=2/2340/000 fio a/b/c=1/1/1 af x/y/z=23/13/666 bl=9
2012-01-20 22:06:54 [quassi] LOG Snd FF oD v/d/n/p=234/5356/2324/34600
I'm concerned only about this in the 2nd line: "x/y/z=23/13/666" I want to do math with the values of x and z (in this case 23 and 666. If I can just get an array going that looks like (23,666,x,z,x,z...)

I must do this in perl (one of my weakest languages) and I'm having trouble getting that section. I've tried something like this but I'm sure I have the split() part all wrong. If you could help with that I think it'd get me going with the rest.


Code:
open(LOGFILE);
foreach $line (<LOGFILE>) {
 ($my_x, $my_z) = split(/x\/y\/z=/, $line)

  ...

}
close(LOGFILE);
 
Old 03-25-2012, 02:13 AM   #2
Σigma
LQ Newbie
 
Registered: Mar 2012
Location: Australia
Distribution: ubuntu/linvo/dsl
Posts: 2

Rep: Reputation: 1
Try
Code:
my ($x, $z) = $line =~ m|x/y/z=(\d+)/\d+/(\d+)|;
 
1 members found this post helpful.
Old 03-25-2012, 03:38 PM   #3
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Original Poster
Rep: Reputation: 16
Thanks Σigma --that works. Can you (or someone) please explain the match used with pipes? I looked around for this and typically I see it used only like so: m/foo/

Also, I had to strip out empty lines, etc. The resulting code is:

Code:
open(LOGFILE, "/tmp/alog");

for $line (<LOGFILE>) {
 if ($line =~ /x\/y\/z/) {
  my ($tbytes, $ttime) = $line =~ m|x/y/z=(\d+)/\d+/(\d+)|;
  chomp $line;
  push(@bytesandtime, $tbytes, $ttime);
 }
}
 
Old 03-25-2012, 04:53 PM   #4
Σigma
LQ Newbie
 
Registered: Mar 2012
Location: Australia
Distribution: ubuntu/linvo/dsl
Posts: 2

Rep: Reputation: 1
“the // default delimiters for a match can be changed to arbitrary delimiters by putting an 'm' out front” - http://perldoc.perl.org/perlrequick.html

Basically, by sticking an “m” in front you can delimit your regex with near anything you feel like. In this case “|” is easier because then you don't have to escape the “/”s.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Which Process is writing to log file ZAMO Linux - General 4 08-17-2016 09:54 PM
Perl that generate log file casualzone Programming 1 12-21-2011 08:58 AM
[SOLVED] Process a log file on an hourly basis but the log only rotates once a day hattori.hanzo Programming 4 10-28-2010 08:55 AM
Strange string in log file ne21 Linux - Security 2 04-18-2004 04:23 AM
Strange Log File Virtus Linux - General 2 02-17-2003 05:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration