Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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…
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…
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…

920 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

12 Experts available now in Live!

Get 1:1 Help Now