将一个文件按照行数,均等的分割成多个小文件,例如,一个550行的文件,分割为每个文件有100行,则将分割为6个小文件
运行结果
参考代码(split_file.pl)
#!/usr/bin/perl
#usage way: split_file.pl test
open(FILE,$ARGV[0]) or die ("Error: cannot open file $ARGV[0].\n");
@lst_line = <FILE>;
my $dir_num = &splitfile(@lst_line);
#print "$dir_num\n";
exit;
################################################################
################################################################
sub splitfile{
my (@input_line) = @_;
my $everyfule_num = 80; #Set partition lines
my $line_num = @input_line;
my $split_num = $line_num/$everyfule_num;
unlink "temp_lst";
open(TEMPA, ">> temp_lst") || die ("Could not open file temp_lst! \n");
my $sq_num = 0;
foreach $line (@input_line){
$line_index ++;
printf TEMPA ("$line");
if($line_index%$everyfule_num == 0){
$sq_num = $line_index/$everyfule_num;
my $lst_name = "test_"."lst_".$sq_num;
rename "temp_lst", "$lst_name";
close TEMPA;
unlink "temp_lst";
open(TEMPA, ">> temp_lst") || die ("Could not open file temp_lst! \n");
}
}
rename "temp_lst", "test_"."lst_".($sq_num+1);
close TEMPA;
return $sq_num+1;
} #end of splitfile
|
请发表评论