[请益] 两份log差异比较

楼主: nash7555 (Caspar)   2020-03-28 23:23:15
各位高手先进~
小弟初学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
作者: herculus6502 (金麟岂是池中物)   2020-03-29 20:37:00
diff是否是你要的?

Links booklink

Contact Us: admin [ a t ] ucptt.com