3

我正在将行与两个文本文件ref.txt(参考)和log.txt. 但是我想忽略的任何一个文件中都可能有任意数量的空行;我怎样才能做到这一点?

参考文件

one

two


three



end

日志.txt

one
two
three
end

输出中不会有不正确的日志行,换句话说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...
}

我当前的代码:

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

6 回答 6

7

如果您在 Linux 平台上,请使用:

diff -B ref.txt log.txt

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

于 2012-07-19T16:48:59.817 回答
2

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

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

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

于 2012-07-19T16:38:57.827 回答
2

这种方式对我来说似乎是最“类似perl”的。没有花哨的循环或任何东西,只需 slurp 文件并 grep 出空白行。

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]);
}
于 2012-07-19T17:06:19.063 回答
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;
    }
}
于 2012-07-19T16:42:57.793 回答
0
man diff

diff -B ref.txt log.txt
于 2012-07-19T16:46:47.810 回答
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
于 2012-07-19T16:47:15.967 回答