各位高手先進~
小弟初學perl, 想以perl撰寫一個比較log差異(file1, file2)的自動化程式
log的內容:
逐行條列error, warning訊息
功能:
兩份log進行逐行比對, 如果有份都有的忽略。
如果出現'訊息內容有差異'或者'多一項error/warrning', 將此訊息抓出並另存
一個新檔中。
目前癥結點:
想做兩個迴圈的方式, 兩個log皆逐行比對。換句話說,file1抓line1與file2
逐行確認, 若有找到相同, 直接忽略。改由file1抓line2與file2逐行確認, 若
file2整份掃完, 皆無相同, 將file1的line2另存新檔, 以此類推。
若高手, 先進有更好寫法, 也可提供。
目前完成的code內容: (###為卡關處......)
#!/usr/bin/perl -w
print 'Enter the name of original file: ';
chomp($original_file = <STDIN>);
print 'Enter the name of new file: ';
chomp($new_file = <STDIN>);
open(ORIF, '<' ,$original_file) or die '\nError: can't open $original_file
file.';
open(NEWF, '<' ,$new_file) or die '\nError: can't open $new_file file.';
open(TEMPORI, '>' , 'temp_$original_file') or die '\nCould not create and
write a temp file for original.';
open(TEMPNEW, '>' , 'temp_$new_file') or die '\nCould not create and write a
temp file for new.';
open(DIFFLIST, '>' ,'difference_list.txt') or die '\nCould not create and
write a temp file for new.';
$cnt_debug = 0;
while (<ORIF>){
my $line = $_;
if ($line =~ /^Warning\s+\(([0-9]+)\):\s+.*$/){
print TEMPORI $line;
}else{
if ($line =~ /^Warning: .*$/){
print TEMPORI $line;
}else{
if ($line =~/^(Critical |\s*)Warning\s+\(([0-9]+)\):\s+.*$/){
print TEMPORI $line;
}else{
if ($line =~ /^Critical Warning: supported quartus
version.*$/){
}else{
if ($line =~ /^Critical Warning: project is run on.*$/){
}else{
if ($line =~ /^Error\s+\(([0-9]+)\):\s+.*$/){
print TEMPORI $line;
}else{
if ($line =~ /^Error: .*$/){
print TEMPORI $line;
}else{
}
}
}
}
}
}
}
}
while (<NEWF>){
my $line = $_;
if ($line =~ /^Warning\s+\(([0-9]+)\):\s+.*$/){
print TEMPNEW $line;
}else{
if ($line =~ /^Warning: .*$/){
print TEMPNEW $line;
}else{
if ($line =~/^(Critical |\s*)Warning\s+\(([0-9]+)\):\s+.*$/){
print TEMPNEW $line;
}else{
if ($line =~ /^Critical Warning: supported quartus
version.*$/){
}else{
if ($line =~ /^Critical Warning: project is run on.*$/){
}else{
if ($line =~ /^Error\s+\(([0-9]+)\):\s+.*$/){
print TEMPNEW $line;
}else{
if ($line =~ /^Error: .*$/){
print TEMPNEW $line;
}else{
}
}
}
}
}
}
}
}
close (ORIF); close (NEWF); close (TEMPORI); close (TEMPNEW);
open(TEMPORI, '<' , 'temp_$original_file') or die '\nCould not read a temp
file for original.';
open(TEMPNEW, '<' , 'temp_$new_file') or die '\nCould not read a temp file
for new.';
###while (<TEMPNEW>){
### my $ind_diff = 0;
### my $line_new = $_;
### print 'NEW_' . $line_new;
### foreach my $line_ori (<TEMPORI>){
### print 'ORI_' . $line_ori;
###
### if ($line_new eq $line_ori){
### $cnt_debug--;
### print $cnt_debug . '\n';
### print $ind_diff . '\n';
### }else{
### $cnt_debug++;
### print $cnt_debug . '\n';
### $ind_diff = 1;
### print $ind_diff . '\n';
### }
### }
###
### if ($ind_diff eq 1){
### print DIFFLIST $line_new . '\n';
### }else{
### }
###}
close (TEMPORI);
close (TEMPNEW);
close (DIFFLIST);
--
The elevator to success is out of order. You'll have to use the stairs one
step at a time.
像搭電梯般快速成功是不切實際的。你必需沿著樓梯,一步一階的往上爬。
Joe Girard
--