为什么生成的输出文件行数不同

时间:2015-04-28 10:02:38

标签: regex linux perl

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,搜索单词count的行。对于每个这样的行,代码然后获取列表的第二个值“:”,并比较file1.a和file1.b中的值 例如:File1:

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.最后一行末尾的新行不存在)

1 个答案:

答案 0 :(得分:2)

如记录所示,wc -l返回换行计数,即换行符数,而不是行数。