Executing awk from perl script

A

AyOut

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?

Thanks.
 
J

John W. Krahn

AyOut said:
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';


You're welcome.



John
 
T

Tintin

AyOut said:
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?


Why bother writing in Perl, if you are essentially writing a shell script?
 
P

Peter J. Holzer

So what? Do you know that it comes with an accompanying Perl module?

I think, that, yes, he knows that, since he answered the question "Why
bother writing in Perl?" with "I'm writing the data to rrdtool". Maybe
he doesn't know that he doesn't have to use Perl for that?

hp
 
G

Greg Bacon

: Abigail Bush wrote:
:
: > But I give you one hint: `` is a double quoted context. So Perl will
: > interpretate variables.
:
: I interpret interpretate as interpolate.

Conversate for a few
'Cuz in a few
We gon' do
....

Sorry, don't mind me.

Greg, hands in the air
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top