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

时间:2012-07-19 16:35:44

标签: perl file blank-line

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

ref.txt

one

two


three



end

log.txt的

one
two
three
end

输出中没有错误的日志行,换句话说log.txtref.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...
}

我目前的代码:

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
    }
}

6 个答案:

答案 0 :(得分:7)

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

diff -B ref.txt log.txt

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

答案 1 :(得分:2)

您可以通过将空白行与此正则表达式进行比较来跳过空行:

next if $line =~ /^\s*$/

这将匹配任何可能构成空白行的空格或换行符。

答案 2 :(得分:2)

这种方式对我来说似乎是最“像perl一样”。没有花哨的循环或任何东西,只是啜饮文件并掏出空白行。

use warnings;

$f1 = "path/file/1";
$f2 = "path/file/2";

open(IN1, "<$f1") or die "Cannot open file: $f1 ($!)\n";
open(IN2, "<$f2") or die "Cannot open file: $f2 ($!)\n";

chomp(@lines1 = <IN1>); # slurp the files
chomp(@lines2 = <IN2>);

@l1 = grep(!/^\s*$/,@lines1); # get the files without empty lines
@l2 = grep(!/^\s*$/,@lines2);

# something like this to print the non-matching lines
for $i (0 .. $#l1) {
   print "[$f1 $i]: $l1[$i]\n[$f2 $i]: $l2[$i]\n" if($l1[$i] ne $l2[$i]);
}

答案 3 :(得分:0)

每次都可以循环查找每一行:

while(1) {
    while(defined($l = <INLOG>) and $l eq "") {}
    while(defined($r = <INREF>) and $r eq "") {}

    if(!defined($l) or !defined($r)) {
        break;
    }

    if($l ne $r) {
        print $l, "\n";
        $boolRef = 0;
    }
}

答案 4 :(得分:0)

man diff

diff -B ref.txt log.txt

答案 5 :(得分:0)

# line skipping code
while (defined($l=<INLOG>) && $l =~ /^$/ ) {}  # no-op loop exits with $l that has length

while (defined($r=<INREF>) && $r =~ /^$/ ) {}  # no-op loop exits with $r that has length