我是perl
的新手。我有一个文本文件,我希望读取该文件并计算以“ 6”开头的行数,并将总数返回到以“ 5”开头的前一行,其中###
在哪里。最后,我想返回以“ 6”开头的所有行的总和,在以“ 9”开头的最后一行中返回######
。我该怎么办这个程序。
ex.txt
120180117674189
500000120180117###000000006000L
6CC0000490 00001013 000001000 000000000003590CNCP0001
6CC0000491 00001007 000001000 000000000003591CNCP0001
6CC0000493 00001002 000001000 000000000003592CNCP0001
6CC0000490 00001013 000001000 000000000003590CNCP0001
6CC0000491 00001007 000001000 000000000003591CNCP0001
6CC0000493 00001002 000001000 000000000003592CNCP0001
500000220180117###000000162420L
6CC0000458 00001019 000078600 000000000003585CNCP0001
6CC0000447 00001019 000005500 000000000003584CNCP0001
6CC0000483 00001005 000001800 000000000003588CNCH0001
6CC0000488 00001005 000023800 000000000003589CNCP0001
6CC0000446 00001005 000052720 000000000003583CNCP0001
500000320180117###000000429631L
61234 00001017 000085831 000000000123456 12345
6123 00001017 000000000 000000000012345 1234
612 00001003 000343800 000000000001234 123
9 ######000000592051
Wex.txt (应输出)
120180117674189
500000120180117006000000006000L
6CC0000490 00001013 000001000 000000000003590CNCP0001
6CC0000491 00001007 000001000 000000000003591CNCP0001
6CC0000493 00001002 000001000 000000000003592CNCP0001
6CC0000490 00001013 000001000 000000000003590CNCP0001
6CC0000491 00001007 000001000 000000000003591CNCP0001
6CC0000493 00001002 000001000 000000000003592CNCP0001
500000220180117005000000162420L
6CC0000458 00001019 000078600 000000000003585CNCP0001
6CC0000447 00001019 000005500 000000000003584CNCP0001
6CC0000483 00001005 000001800 000000000003588CNCH0001
6CC0000488 00001005 000023800 000000000003589CNCP0001
6CC0000446 00001005 000052720 000000000003583CNCP0001
500000320180117003000000429631L
61234 00001017 000085831 000000000123456 12345
6123 00001017 000000000 000000000012345 1234
612 00001003 000343800 000000000001234 123
9 000014000000592051
新文件
120180117674189
500000120180117006000000006000L
6CC0000490 00001013 000001000 000000000003590CNCP0001
6CC0000491 00001007 000001000 000000000003591CNCP0001
6CC0000493 00001002 000001000 000000000003592CNCP0001
6CC0000490 00001013 000001000 000000000003590CNCP0001
6CC0000491 00001007 000001000 000000000003591CNCP0001
6CC0000493 00001002 000001000 000000000003592CNCP0001
500000220180117 ### 000000162420L
6CC0000458 00001019 000078600 000000000003585CNCP0001
6CC0000447 00001019 000005500 000000000003584CNCP0001
6CC0000483 00001005 000001800 000000000003588CNCH0001
6CC0000488 00001005 000023800 000000000003589CNCP0001
6CC0000446 00001005 000052720 000000000003583CNCP0001
500000320180117 ### 000000429631L
61234 00001017 000085831 000000000123456 12345
6123 00001017 000000000 000000000012345 1234
612 00001003 000343800 000000000001234 123
9 ###### 000000592051
答案 0 :(得分:0)
以下是如何完成此操作的示例:
use feature qw(say);
use strict;
use warnings;
use List::Util qw(sum);
my $fn = 'ex.txt';
open ( my $fh, '<', $fn ) or die "Could not open file '$fn': $!";
my @lines;
my @count;
my $cur_count = undef; # undef --> before first line starting with 6
my $flag = 2;
while (my $line = <$fh>) {
chomp $line;
push @lines, $line;
if ( $line =~ /^6/) {
$cur_count = 0 if !defined $cur_count;
$flag = 2 if $flag == 1;
$cur_count++;
}
elsif ($line =~ /^5/ ) {
push @count, $cur_count if (defined $cur_count) && $flag == 2;
$cur_count = 0;
$flag = 1;
}
else {
$flag = 2 if $flag == 1;
}
}
push @count, $cur_count if defined $cur_count;
close $fh;
my $total = sum @count;
my $j = 0;
for (@lines) {
if (/^(5[^#]*)[#]+(.*)$/) {
say $1, (sprintf "%06d", $count[$j++]), $2;
}
elsif (/^(9[^#]*)[#]+(.*)$/) {
say $1, (sprintf "%06d", $total), $2;
}
else {
say;
}
}
编辑:
要从脚本中写入文件,请执行以下操作:
# [...]
my $savefn = 'output.txt';
open ( my $sfh, '>', $savefn ) or die "Could not open file '$savefn': $!";
my $j = 0;
for (@lines) {
if (/^(5[^#]*)[#]+(.*)$/) {
say $sfh $1, (sprintf "%06d", $count[$j++]), $2;
}
elsif (/^(9[^#]*)[#]+(.*)$/) {
say $sfh $1, (sprintf "%06d", $total), $2;
}
else {
say $sfh $_;
}
}
close $sfh;