用于文件解析的perl代码

用于文件解析的perl代码,perl,Perl,如何使用perl根据文件行中的某种模式将一个非常大的文件拆分为许多小文件 例文件: CONECT 592 593 594 CONECT 595 596 597 CONECT 597 598

如何使用
perl
根据文件行中的某种模式将一个非常大的文件拆分为许多小文件

例文件:

CONECT  592  593  594                                                           
CONECT  595  596  597                                                           
CONECT  597  598                                                                
END                
CONECT  591  593  594                                                           
CONECT  595  596  596                                                           
CONECT  597  598                                                                
END
CONECT  592  593  594                                                           
CONECT  594  596  598                                                           
CONECT  597  598                                                                
END        

我必须在一个文件上制作多个单独的文件。输出文件的起始行应为“
CONECT
”,结束行应为“
end
”。这是一个大文件(1gb)

这是一个小算法,你可以试试。请让我知道,如果你需要任何明确的代码

while (<FD>)
{
   if ($_ =~ /^END/)
   {
      # save buffer in new file.
      # reset buffer.
   }
   # add line to buffer.
}
while()
{
如果($\=~/^END/)
{
#在新文件中保存缓冲区。
#重置缓冲区。
}
#将行添加到缓冲区。
}
#/usr/bin/perl
严格使用;
我的$file1='file_2b_read.txt';
我的$File2='newfile_2b_created.txt';
打开(CMD,“$File2”;
我的$cnt=1;
while(){
打印输出$;
/^完/做{
#创建新文件
$cnt++;
关闭(输出);
$File2='newfile_2b_创建'$cnt.'.txt';
打开输出“>$File2”;
下一个
};
}
关闭(CMD);

希望这将帮助您

一个更干净的版本,使用更现代的perl(使用lexcial文件句柄打开三个参数,检查调用
打开
)的错误)

!/usr/bin/perl
严格使用;
使用警告;
my$in_file='file_2b_read.txt';
my$out_file='newfile_2b_part_%06d.txt'#输出文件名模板
我的$counter=1;
打开我的$in_fh',sprintf($out_file,$counter)或die$!;
而(){
打印$out\u fh$\u;
如果(/^END/){
收尾($out\fh);
打开$out\u fh,'>',sprintf($out\u文件,++$counter)或die$!;
}
}
#事后清理
关闭$OFH;
以欧元结算美元;

基于dgw的答案,但经过修改,不会创建虚假的最终文件:

#!/usr/bin/perl

use strict;
use warnings;

my $in_file = 'file_2b_read.txt';
my $out_file_template = 'newfile_2b_part_%06d.txt';
my $counter = 1;

open my $in_fh , '<' , $in_file or die $!;
my $out_fh;

while ( <$in_fh> ) {
    if (!$out_fh) {
        open $out_fh , '>' , sprintf( $out_file_template, $counter++ ) or die $!;
    }
    print $out_fh $_;

    if ( /^END/ ) {
        close( $out_fh );
        $out_fh = undef;
    }
}

# cleanup afterwards
if ($out_fh) { close( $out_fh ) }
close $in_fh;
!/usr/bin/perl
严格使用;
使用警告;
my$in_file='file_2b_read.txt';
我的$out_文件_模板='newfile_2b_part_u%06d.txt';
我的$counter=1;
打开我的$in_fh',sprintf($out_file_模板,$counter++)或die$!;
}
打印$out\u fh$\u;
如果(/^END/){
收尾($out\fh);
$out\u fh=未定义;
}
}
#事后清理
如果($out\u fh){close($out\u fh)}
以欧元结算美元;
#!/usr/bin/perl

use strict;
use warnings;

my $in_file  = 'file_2b_read.txt';
my $out_file = 'newfile_2b_part_%06d.txt'; # Template for output filenames
my $counter  = 1;

open my $in_fh , '<' , $in_file or die $!;
open my $out_fh , '>' , sprintf( $out_file , $counter ) or die $!;

while( <$in_fh> ) {
  print $out_fh $_;

  if( /^END/ ) {
    close( $out_fh ) ;
    open $out_fh , '>' , sprintf( $out_file , ++$counter ) or die $!;
  }
}

# cleanup afterwards
close $out_fh ;
close $in_fh ;
#!/usr/bin/perl

use strict;
use warnings;

my $in_file = 'file_2b_read.txt';
my $out_file_template = 'newfile_2b_part_%06d.txt';
my $counter = 1;

open my $in_fh , '<' , $in_file or die $!;
my $out_fh;

while ( <$in_fh> ) {
    if (!$out_fh) {
        open $out_fh , '>' , sprintf( $out_file_template, $counter++ ) or die $!;
    }
    print $out_fh $_;

    if ( /^END/ ) {
        close( $out_fh );
        $out_fh = undef;
    }
}

# cleanup afterwards
if ($out_fh) { close( $out_fh ) }
close $in_fh;