Perl文件简单合并


将两个文本文件合并起来,同一行并为一行,使用'|'连接,对行首和空格进行移除,下面的代码就是将a,b两个文件合并到文件c。
use FileHandle;
use feature ":5.10";
$fa=FileHandle->new("a", "r");
$fb=FileHandle->new("b", "r");
$fc=FileHandle->new("c", "w");
my ($is_gon,$i)=(1,0);
#left trim function to remove leading whitespace
sub ltrim($){
my $string = shift;
$string =~ s/^\s+//;
return $string;
}
#合并主体程序,这里只是将其打印出来,合并到文件c的操作很简单,可以自行添加
sub getfilemerged{
state $i;
#say $i;
my ($a,$b)=($fa->getline,$fb->getline);
$is_gon=0 if($fa->eof and $fb->eof);
chomp($a,$b);
say ltrim($a.'|'.$b);
$i++;
}
while($is_gon){
getfilemerged;
}
END{
$fa->close;
$fb->close;
$fc->close;
}
use FileHandle;
use feature ":5.10";
$fa=FileHandle->new("a", "r");
$fb=FileHandle->new("b", "r");
$fc=FileHandle->new("c", "w");
my ($is_gon,$i)=(1,0);
#left trim function to remove leading whitespace
sub ltrim($){
my $string = shift;
$string =~ s/^\s+//;
return $string;
}
#合并主体程序,这里只是将其打印出来,合并到文件c的操作很简单,可以自行添加
sub getfilemerged{
state $i;
#say $i;
my ($a,$b)=($fa->getline,$fb->getline);
$is_gon=0 if($fa->eof and $fb->eof);
chomp($a,$b);
say ltrim($a.'|'.$b);
$i++;
}
while($is_gon){
getfilemerged;
}
END{
$fa->close;
$fb->close;
$fc->close;
}