使用Perl分割文件

使用Perl分割文件

特性


使用换行作为分界
忽略注释行#
分割存入新指定的文件中

待分割的文件test.lst


wwdg/prescaler

syscfg/test1
syscfg/test2

uart/uart7/uart7_dma1
uart/uart7/uart7_dma2

分割后的文件结构


847278-20170605204728497-1185041714.png

Perl代码genlst.pl


#!/usr/bin/perl
use warnings;

###########################################
#cut up file
#
#cut_flie.pl test.lst
###########################################

my $find_ip =0;
my $find_begin = 0;
my $line_index = 0;
my $lstname   = "error";
my $past_line = "error";

my $infile= $ARGV[0];
open(INFILE, " $infile") || die ("Could not open file $infile ! \n");

if(-z $infile){
    print "\nFile is blank!\n";
    exit;
}

#覆盖lst中的文件,需手动删除已经存在的文件
my $dir = "lst";                        #创建存放目录
if(-e $dir){
}
else{
    mkdir $dir;
}

#删除lst中的文件
#my $dir = "lst";                        #创建存放目录
#if(-e $dir){
#    unlink glob "$dir/* $dir/.*"
#}
#else{
#    mkdir $dir;
#}


# get file lines amount 
my @fileline = <INFILE>;                #得到文件的行数
my $line_num = @fileline;
close INFILE;

unlink "temp.lst";
open(TEMPA, ">> temp.lst") || die ("Could not open file temp.lst! \n");

open(INFILE, " $infile") || die ("Could not open file $infile ! \n");
while ($line = <INFILE>){
    $line_index ++;
    chomp($line);
    if($line =~ /^#/) { #find "#" at the beginning, don't care
        next;
    }

    if($line =~ /^(\w+)[\s\$\W]*/){     #匹配捕获字符串,非空行
        if($past_line =~ /^$/){         #上一行为空最为分界
            print TEMPA  "\n\n";

            my $full_lstname = $lstname."_lst";  
            print "$dir\/$full_lstname\n";
            
            rename "temp.lst",  "$full_lstname";
            close TEMPA;                #完成一个文件的分割

            system"mv $full_lstname lst";

            print "\n\n";

            unlink "temp.lst";
            open(TEMPA, ">> temp.lst") || die ("Could not open file temp.lst! \n");  #继续进行下一个文件处理
        }

        $line =~ /^(\w+)[\s\$\W]*/ ;    #正则处理,得到文件名
        print TEMPA  "$line \n";
        print "$line \n";
        $lstname = $1;
    }

    if($line_index == $line_num){       #单独处理最后行情况
        print TEMPA  "\n\n";
        #print "\n\n";

        my $full_lstname = $lstname."_lst";
        print "$dir\/$full_lstname\n";
        
        rename "temp.lst",  "$full_lstname";
        close TEMPA;

        system"cp $full_lstname lst";
        unlink "$full_lstname";
    }

    if($line =~ /^\s*$/){
        if($line_index == 1){
            next;
        }
        if($past_line =~ /^$/){
            next;
        }
    }
    $past_line = $line;                   #保存上一行情况
}
close INFILE;
close TEMPA;
unlink "temp.lst";                        
unlink "lst/error_lst";

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值