[Perl] search log v1.0
作者: reistlin
来源: http://www.reistlin.com/blog/1
更新时间: 2010.06
版权声明: 原创文章.转载请保留作者信息和原文完整.谢绝任何方式的摘要
#!/usr/bin/perl -w # Name: search log v1.0 # Author: reistlin # Website: www.reistlin.com # Hotfix: bigyong # Website: www.bigyong.com # Date: 2010.06.01 # BTW: reistlin`s first perl script :-) use strict; use Data::Dumper; # Debug Switch my $debug = 0; # start time my $time_1 = time(); # clear system "clear"; # Keyword mode my $key_file = "key.txt"; my $key_index = 0; my @key_load; if ( ! -e $key_file ) { print "[Error] \[$key_file\] File does not exist. \n"; exit; } else { print "[INFO] Load \[$key_file\] Check OK ...\n" } # key file details my @key_stat = stat ($key_file); print "[INFO] $key_file File Details: \n"; print "UID: $key_stat[4] \n"; print "GID: $key_stat[5] \n"; print "Size: $key_stat[7] bytes \n"; print "Atime: $key_stat[8] \n"; print "Mtime: $key_stat[9] \n"; print "\n"; open (IN, $key_file); while (<IN>) { my $key_line = $_; chomp $key_line; $key_load[$key_index] = $key_line; $key_index++; } # Search mode my $log_file = "log.log"; my $log_index = 0; my @log_load; if ( ! -e $log_file ) { print "[Error] \[$log_file\] File does not exist. \n"; exit; } else { print "[INFO] Load \[$log_file\] Check OK ...\n" } # log file details my @log_stat = stat ($log_file); print "[INFO] $log_file File Details: \n"; print "UID: $log_stat[4] \n"; print "GID: $log_stat[5] \n"; print "Size: $log_stat[7] bytes \n"; print "Atime: $log_stat[8] \n"; print "Mtime: $log_stat[9] \n"; print "\n"; open (IN, $log_file); my $key_load = join("|", @key_load); while (<IN>) { my $log_line = $_; chomp $log_line; $log_index++; if ($log_line =~ /$key_load/) { $log_line = "\[$log_index\] $log_line \n"; push @log_load, $log_line; } } # Write Mode my $result_file = "result.log"; open (IN, "+>" . $result_file); for ($log_index = 0; $log_index < @log_load; $log_index++) { print IN "$log_load[$log_index] \n"; } if (-e $result_file) { print "[INFO] Success \[$result_file\] Saved ... \n"; } # result file details my @result_stat = stat ($result_file); print "[INFO] Result Size: $result_stat[7] bytes \n"; # end time my $time_2 = time(); my $time = $time_2 - $time_1; # script runtime print "[INFO] Runtime \[$time\] Second ... \n";