[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2010-11-29
5
Medium Priority
?
449 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
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…
Six Sigma Control Plans

872 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