?
Solved

RE:  Need PERL script to parse a log file from the last 24 hours

Posted on 2010-11-29
5
Medium Priority
?
446 Views
Last Modified: 2013-11-13
Hi,
   I'd like someone to assist me with creating a PERL script that will parse a log file based on time (initially I will start with the last 24 hours).  The file will look like this:


[2010-11-29 12:05:29,391 -0800]    "6 digit number" "text"

I hope this is enough information.
0
Comment
Question by:unix_admin_1234
[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
  • 2
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
jeromee earned 1000 total points
ID: 34235067
Try this:
perl -MPOSIX -ne'BEGIN{$since=strftime("%Y-%m-%d %H:%M:%S", localtime(time-(3600*24)))} print if substr($_,1) ge $since' your_file_here
0
 

Author Comment

by:unix_admin_1234
ID: 34237395
Is there any way to put this in a script as opposed to a one-liner?  I need to incorporate this into a larger script?  Also, a quick summary of this small script would be great.

Thanks!  It look promising though I haven' tried it yet.
0
 
LVL 3

Assisted Solution

by:chrios
chrios earned 1000 total points
ID: 34238859
This is basically a script version of jeromee's oneliner. Since I changed the output from strftime we dont need to cut (substr) anything before comparing the time to the lines from the log, so this should be a bit easier to understand.

ge = greater or even, string compare

#!/usr/bin/perl -w
use POSIX qw(strftime);

$file = "myfile.txt";
$datestr=strftime("[%Y-%m-%d %H:%M:%S]", localtime(time-3600*24));

open(FH, "<$file") or die "Could not open $file: $!\n";

while(<FH>)
{
    print if $_ ge $datestr;
}

close(FH);

Open in new window

0
 
LVL 10

Expert Comment

by:jeromee
ID: 34240531
chrios's "translation" looks good.
I would have added a "use strict" and "my" since it's a good practice.


#!/usr/bin/perl -w

use strict;
use POSIX qw(strftime);

my $file = "myfile.txt";
my $datestr = strftime("[%Y-%m-%d %H:%M:%S]", localtime(time-3600*24));

open(FH, "<$file") or die "Could not open $file: $!\n";

while(<FH>) {
    print if $_ ge $datestr;
}

close(FH);

Open in new window

0
 

Author Comment

by:unix_admin_1234
ID: 34477785
Thanks everyone..Both scripts seem to work sorry for the late reply.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Simple Linear Regression
Starting up a Project

770 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