Solved

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

Posted on 2010-11-29
5
428 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

20 Experts available now in Live!

Get 1:1 Help Now