in reply to Re^4: print lines withing specific date range from maillog file
in thread print lines withing specific date range from mailoog file

my $start = Time::Piece->strptime('$yesterday', $format);

Please read perlsyn on quotes and quote-like operators. Single quotes do not interpolate. You should use instead:

my $start = Time::Piece->strptime($yesterday, $format);

Replies are listed 'Best First'.
Re^6: print lines withing specific date range from maillog file
by theravadamonk (Scribe) on Mar 30, 2016 at 11:35 UTC

    Hi Monks, I finally suceeded in doing it. it works as expected. Script (my_script.pl) is given below.

    #!/usr/bin/perl use strict; use warnings; use Time::Piece; use POSIX qw(strftime); my $format = '%b %e %T'; my $today = time; my $formatted_today = strftime "%b %e %T", ( localtime($today) ); my $yesterday = $today - 60 * 24 * 60; my $formatted_yesterday = strftime "%b %e %T", ( localtime($yesterday) + ); #print "VAR1 TODAY $today\n"; #print "VAR2 YESTERDAY $yesterday\n"; #print "\n"; #print "formatted_TODAY $formatted_today\n"; #print "formatted_YESTERDAY $formatted_yesterday\n"; #print "\n"; my $start = Time::Piece->strptime($formatted_yesterday, $format); my $end = Time::Piece->strptime($formatted_today, $format); print "VARIABLE END TODAY $end\n"; print "VARIABLE START YESTERDAY $start\n"; print "\n"; open(FILE, "/var/log/maillog") or die "Couldn't open maillog: $!; abor +ting"; while (<FILE>) { my ($timestamp) = /(^\w+\s+\d+\s+\d\d:\d\d:\d\d)/; my $t = Time::Piece->strptime($timestamp, $format); print if $t >= $start && $t <= $end; } close(FILE);