Solved

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

Posted on 2010-11-29
5
434 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
  • 2
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
jeromee earned 250 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 250 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# code editing and collaboration 3 109
SUM 2 INTEGER ARRAYS INTO 1 10 99
Math Equation 23 82
Help Required 2 29
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

816 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

10 Experts available now in Live!

Get 1:1 Help Now