Hello,
Hello,
I have a script that I want to print only if between 08:00 and 17:00. Would I match
> every hour then print?? Any cleaner way to do this would be great. Thanks
Cleaner? Yes, use appropriate whitespace and indentation and remove redundant code. :-)
my($sec,$min,$hour,$mday,$mon);
($sec,$min,$hour,$mday,$mon)=localtime;
$timestamp=sprintf("%3s %02d %02d:%02d:%02d",$Month[$mon],$mday,$hour,$min,$sec);
my $pidstring="pid(".$$."),";
while(<STDIN>){
$line = $_;
if($line=~/OK/){
open( OKLIST,">>/var/log/log.ok");
print OKLIST $timestamp,":",$pidstring,$line;
close(OKLIST);
}else{
if ( $hour =~ /^(08:|09:|10:|11:|12:|13:|14:|15:|16:|17:)/){ open( ERRLIST,">>/var/log/err.ok"); print ERRLIST $timestamp,":",$pidstring,$line; close(ERRLIST); chomp($line); } } } exit;
Something like:
my ( $sec, $min, $hour, $mday, $mon ) = localtime;
my $timestamp = sprintf '%3s %02d %02d:%02d:%02d', $Month[ $mon ], $mday, $hour, $min, $sec;
my $hour = sprintf '%d%02d', $hour, $min;
my $pidstring = "pid($$),";
open my $LOG, '>>', '/var/log/log.ok' or die "Cannot open log: $!"; open my $ERR, '>>', '/var/log/err.ok' or die "Cannot open err: $!";
while ( <STDIN> ) { my $line = "$timestamp:$pidstring$_"; if ( /OK/ ) { print $LOG $line; } elsif ( $hour >= 800 and $hour <= 1700 ) { print $ERR $line; } } exit 0;
__END__
John -- use Perl; program fulfillment
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>