Perl按行分割文件

Perl按行分割文件

将一个文件按照行数,均等的分割成多个小文件,例如,一个550行的文件,分割为每个文件有100行,则将分割为6个小文件

运行结果

847278-20171018142851787-249776263.png
847278-20171018142908646-1095992501.png

参考代码(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

转载于:https://www.cnblogs.com/OneFri/p/7686514.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值