比较两个文件时,如何跳过(忽略)空白行?

jer*_*h91 3 perl file blank-line

我正在比较两行文本文件的行,ref.txt(参考)和log.txt.但是在这两个文件中可能有任意数量的空白行我想忽略; 我怎么能做到这一点?

ref.txt

one

two


three



end
Run Code Online (Sandbox Code Playgroud)

log.txt的

one
two
three
end
Run Code Online (Sandbox Code Playgroud)

输出中没有不正确的日志行,换句话说log.txt匹配ref.txt.

我想用伪代码完成的事情:

while (traversing both files at same time) {
    if ($l is blank line || $r is blank line) {
        if ($l is blank line)
            skip to next non-blank line
        if ($r is blank line)
            skip to next non-blank line
    }
    #continue with line by line comparison...
}
Run Code Online (Sandbox Code Playgroud)

我目前的代码:

use strict;
use warnings;

my $logPath    = ${ARGV [0]};
my $refLogPath = ${ARGV [1]} my $r;    #ref log line
my $l;                                 #log line

open INLOG, $logPath    or die $!;
open INREF, $refLogPath or die $!;

while (defined($l = <INLOG>) and defined($r = <INREF>)) {
    #code for skipping blank lines?
    if ($l ne $r) {
        print $l, "\n";                #Output incorrect line in log file
        $boolRef = 0;                  #false==0
    }
}
Run Code Online (Sandbox Code Playgroud)

JRF*_*son 7

如果您使用的是Linux平台,请使用:

diff -B ref.txt log.txt
Run Code Online (Sandbox Code Playgroud)

-B选项会导致仅插入或删除空行的更改被忽略