AyOut wrote:
> I'm new to perl and have the following line that I try to execute:
> ...
> my $hm = `date +%H:%M --date "1 minute ago"`;
> my $fname = `date +AppName_stats.log.%Y-%m-%d --date "1 days ago"`;
> my $hits = `find /logs/server_name0?/dir/. -name "$fname*" -exec
> gunzip -c {} ;\ | /grep SearchStr | /bin/awk -F","
> '{if(substr($1,12,5)~hmpoint)print $3}' hmpoint=$hm | /bin/awk '{tot+=
> $1}END{print tot}'`;
> When I run this I get all kinds of complaints on the $hits line. I've
> tried various modifications, but can't seem to get it to work. Any
> idea of what's wrong here?
In Perl you would write that as:
use POSIX 'strftime';
use File::Find;
use Compress::Zlib;
my $hm = strftime '%H:%M', localtime $^T - 60;
my $fname = strftime 'AppName_stats.log.%Y-%m-%d', localtime $^T - 86400;
my $hits;
find sub {
return unless /\A\Q$fname/;
my $gz = gzopen( $_, 'rb' ) or die "Cannot open $_: $gzerrno\n";
while ( $gz->gzreadline( my $line ) > 0 ) {
if ( $line =~ /SearchStr/ && substr( $line, 11, 5 ) eq $hm ) {
$hits += ( split /,/, $line )[ 2 ];
}
}
die "Error reading from $_: $gzerrno\n" if $gzerrno != Z_STREAM_END;
$gz->gzclose();
}, glob '/logs/server_name0?/dir';
> Thanks.
You're welcome.
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall