第一次写perl

第一次写perl

要求:
2台web机器日志/var/log/http120080102-access.log /var/log/http220080102-accesslog
有文件/etc/test.conf 有一行 LogFile="abcdefghijklmn20080304"
取得日期$awsday = 04
如果脚本执行日期 $myday 大于 $awsday. 则
去掉两台web机器log文件里有互相访问的记录. (去掉含有 "1.2.3.4 4.3.2.1"或"4.3.2.1 1.2.3.4"的行)
然后把剩下记录写入httplog(/var/log/http_log20080*0*)
第一次写 肯定很烂
有没有高手愿意帮小弟简化或重写下  3q

[Copy to clipboard] [ - ]
CODE:
#!/usr/bin/perl

open (myfile,"/etc/awstats/awstats.test.conf");

($sec,$min,$hr,$day,$mon,$year)=localtime();
$mon=1+$mon;
if($day =~ /[0-9]{1}/)
{
  $myday="0".$day;
}

if( $mon =~ /[0-9]{1}/ )
{
  $mymon="0".$mon;
}

$myyear=1900+$year;
while($awsday=<myfile>)
{
  if ($awsday =~ s/(^LogFile.*)([0-9]{6})([0-9]{2})(\")/$3/sg)
  {
    $nextday=$awsday+1;
    if ( $nextday =~ /[0-9]{1} )
    {
      $nextday="0".$nextday;
    }
    while ( $myday >= $nextday )
    {
      if ( !-e "/var/log/http1$myyear-$mymon-$nextday-access.log" )
      {
        print "log file http1$myyear-$mymon-$nextday-access.log not exist";
        exit;
      }

      if ( !-e "/var/log/http2$myyear-$mymon-$nextday-access.log" )
      {
        print "log file http2$myyear-$mymon-$nextday-access.log not exist";
        exit;
      }
      open (1file,"/var/log/http1$myyear-$mymon-$nextday-access.log ");
      open (2file,"/var/log/http2$myyear-$mymon-$nextday-access.log ");
      open (httplog,"<</var/log/http_log$myyear$mymon$nextday"
      while( $file1=<1file> )
      {
        if ( !$file1 =~ /1.2.3.4 4.3.2.1/ && !$file1 =~ /4.3.2.1 1.2.3.4/ )
        {
          print httplog $file1;
        }
      }
      while( $file2=<2file> )
      {
        if ( !$file2 =~ /1.2.3.4 4.3.2.1/ && !$file2 =~ /4.3.2.1 1.2.3.4/ )
        {
          print httplog $file2;
        }
      }
      close 1file;
      close 2file;
      close httplog;
    }
  }
  exit;
}
close myfile;

不知道这样是不是符合你的要求
#!/usr/bin/perl


use strict;
use warnings;

my $conf_file = '/etc/awstats/awstats.test.conf';

open CONF, "<$conf_file"
    or die "Can not open $conf_file: $!\n";

my @time = localtime;

my $day  = $time[3] + 1;
my $mon  = $time[4] + 1;
my $year = $time[5] + 1900;

while (<CONF>) {
    chomp;
    my $awsday = $1 if /^LogFile\D*\d{6}(\d+)/;
    
    if ($day >= $awsday) {
        my $log_file1 = sprintf "%s%d%02d%02d%s", "/var/log/http1", $year, $mon, $awsday, "-access.log";
        my $log_file2 = sprintf "%s%d%02d%02d%s", "/var/log/http2", $year, $mon, $awsday, "-access.log";
        my $httplog = sprintf "%s%d%02d%02d", "/var/log/http_log", $year, $mon, $awsday;
        
        open LOG1, "<$log_file1"
            or die "Can not open $log_file1: $!\n";
        open LOG2, "<$log_file2"
            or die "Can not open $log_file2: $!\n";
        open HTTP_LOG, ">>$httplog"
            or die "Can not open $httplog: $!\n";

        while (<LOG1>) {
            chomp;
            next if /1.2.3.4 4.3.2.1/;
            next if /4.3.2.1 1.2.3.4/;
            print HTTP_LOG;
        }
        while (<LOG2>) {
            chomp
            next if /1.2.3.4 4.3.2.1/;
            next if /4.3.2.1 1.2.3.4/;
            print HTTP_LOG;
        }
    }
}


good
thanks