比较两个文件时,如何跳过(忽略)空白行?
我正在对两行文本文件ref.txt
(参考)和log.txt
进行比较。但是任何一个文件中可能会有任意数量的空白行,我想忽略它们;我怎么能做到这一点?比较两个文件时,如何跳过(忽略)空白行?
ref.txt
one
two
three
end
log.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
}
}
您可以通过比较这正则表达式跳过空白行:
next if $line =~ /^\s*$/
这将匹配任何可能构成空行的空格或换行符。
您可以循环找到的每一行,每一时间:
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;
}
}
# 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
如果你是一个Linux平台上,使用:
diff -B ref.txt log.txt
的-B
选项导致的变化只需插入或删除空白行即可忽略
这种方式似乎对我来说是最“类似perl”的。没有花哨的循环或任何东西,只是啜泣的文件和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]);
}
也许重写这些greps为'@ l1 = grep(/ \ S /,@ lines1)'等 – 2012-07-20 10:35:32
如何从@ l1和@ l2中检索单行? – jerryh91 2012-07-20 15:47:34
这并不完美,因为一条不匹配的行会使所有位于下面的行不匹配。我以为我会分享这个作为perl的文件啜泣/ grepping能力的探索。如果可以的话,肯定只是使用'diff -B'。 – kevlar1818 2012-07-20 16:21:23
对我来说,写下'除非$ line =〜/ \ S /'似乎更容易理解(至少对我来说)。 – 2012-07-20 10:34:46
@DaveCross我想你的版本可以确保在线读取有一些东西。总是有更多的方法来完成它INT Perl! – squiguy 2012-07-20 13:05:38
是的。在处理一个太多的“空行”实际上包含空格和/或制表符的文件之后,我切换到了我的方法。 – 2012-07-20 13:11:34