2015-04-28 58 views
0
use Data::Dumper qw(Dumper); 
use strict; 
use warnings; 
die "Usage: $0 FILENAMEs" if not @ARGV; 
my $ni= 10; 
my $fi=10; 
my $flag =0; 
my $ni1=10; 
my $fi1 =10; 
foreach my $file (@ARGV) { 
open my $fh,"<$file.a" or die; 
while (my $line = <$fh>) { 
if ($line =~ /count/) { 
my @ores = split /:/, $line, 2; 
if($file eq "k4" and $flag ==1) 
{ 
    $ni1= $ores[1]; 
} 
else 
{ 
$flag=$flag+1; 
$ni= $ores[1]; 
print "NI--", $ni; 
} 
} 
} 
close $fh; 
$flag=0; 
open $fh,"<$file.b" or die; 
while (my $line = <$fh>) { 
if ($line =~ /count/) { 
my @ores = split /:/, $line, 2; 
if($file eq "k4" and $flag ==1) 
{ 
    $flag = $flag+1; 
    $fi1= $ores[1]; 
} 
else 
{ 
    $fi=$ores[1]; 
print "FI--", $fi; 
} 
} 
} 
    close $fh; 
    open($fh, ">>$file.log") or die "Could not open file '$file'.log $!"; 
if($file eq "k4") 
{ 
print $fh " First Instance of K4 \n-----------------------------------\n"; 
    } 
if($ni > $fi) 
{ 
    print $fh " FI: ", $fi; 
    print $fh " NI: ", $ni; 
    print $fh " NI is more than FI by: ", $ni-$fi; 
    } 
else 
{ 
    print $fh " FI: ", $fi; 
    print $fh " NI: ", $ni; 
    print $fh " FI is more than NI by: ",$fi-$ni; 
    } 
if($file eq "k4") 
{ 
print $fh "\n Second instance of K4"; 
if($ni1 > $fi1) 
{ 
    print $fh " FI: ", $fi1; 
    print $fh "\n NI: ", $ni1; 
    print $fh " NI is more than FI by: ", $ni1-$fi1; 
    } 
    else 
    { 
    print $fh " FI: ", $fi1; 
    print $fh "\n NI: ", $ni1; 
    print $fh " FI is more than NI by: ",$fi1-$ni1; 
    } 
    } 
    } 

上面的代码需要一个文件file1.a和file1.b,搜索带有单词计数的行。对于每个这样的线,代码然后获取由分割列表的第二值“:”和值从file1.a和file1.b比较 例如:文件1:为什么生成的输出文件的行数不同

Loops count : 12345 

文件2:

Roots count : 45679 

输出文件(file1.log):

FI: 45679 
NI: 12345 
FI is more than NI by: 33334 

但是,为什么会发生这种情况?

$wc -l file1.log 
2 file1.log 

(线数应3.在最后一行的末尾的新线是不存在)

回答

2

作为记载,wc -l返回换行符计数,即,换行的数量,而不是数量线。

相关问题