gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:32:29 25 4
gpt4 key购买 nike

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

最佳答案

如文档所述,wc -l 返回换行数,即换行数,而不是行数。

关于regex - 为什么生成的输出文件的行数不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29916285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com