获取每天早上9:00到21:00每小时的随机100条日志

发布时间:2019-08-24编辑:脚本学堂
perl脚本-获取每天早上9:00到21:00每小时的随机100条日志

perl脚本-获取每天早上9:00到21:00每小时的随机100条日志,完整代码。
 

复制代码 代码如下:

#!/usr/bin/perl
use strict;
use warnings;

our $logfile = "/var/log/messages";
our $curdir = `pwd`;
chomp $curdir;
our $curdate = `date '+%Y_%m_%d'`;
our $savefile = "$curdir/savelog.$curdate";

open (FDIN,"< $logfile");
open (FDOUT,"> $savefile");

while (my $pline = <FDIN> ) {
 no strict;
 my @tnum = split (/ |:/,$pline,4);
 unshift @{$tnum[2]},$pline;
}

close FDIN;

foreach (9..21) {
 no strict;
 my $llength = scalar @$_;
 if ($llength <= 100){
  print FDOUT $_ foreach (@$_);
 }
 else {
  my $sum=0;
  while($sum <= 99){
   my $nr = int(rand($llength));
   if (defined(${$_}[$nr])){
print FDOUT ${$_}[$nr];
delete ${$_}[$nr];
++$sum;
   }
  }
 }
}