歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> PERL編程

Perl切割日志的利器

用了過awk,python,perl都切過日志,還是感覺perl是切日志的高手,執行效率高,功能也足夠強大。下面是一個小例子:

open(FILE,"/data/ebi/meta/log");
open(CONF,"/data/ebi/conf/urltype.txt");
open(LOG,">/data/ebi/meta/log_2");
@array1=();
while(<CONF>) {
         chomp;
         @array2 = split(/,/, $_);
         if(scalar(@array2)>=6){
                push @array1,[@array2];
         }
}

close(CONF);

 


while(<FILE>)
{
  chomp;
  my ($suv,$times,$ip,$url,$refer,$type1) = split(/\s+/, $_);
  $type=$type1;
  my (undef,undef,$site) = split(/\//, $url);
  if($urlsitemap{$url}){
         $site = $urlsitemap{$url}[0];
         $prod = $urlsitemap{$url}[1];
         $fun = $urlsitemap{$url}[2];
  }
  else{
          for $i (0 .. $#array1){ 
                $regx = @{$array1[$i]}[1]; 
                if($url=~/$regx/){
                        $site = @{$array1[$i]}[2];
                        $prod = @{$array1[$i]}[3];
                        $fun = @{$array1[$i]}[4];
                        $urlsitemap{$url}=[$site,$prod,$fun];
                        last;
                }
          }
  }


print LOG "$site $suv $ip $url $refer,$type\n ";

}

close(FILE);


close(LOG);

Copyright © Linux教程網 All Rights Reserved