• 使用Perl分割文件


    使用Perl分割文件

    特性


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

    待分割的文件test.lst


    wwdg/prescaler
    
    syscfg/test1
    syscfg/test2
    
    uart/uart7/uart7_dma1
    uart/uart7/uart7_dma2
    

    分割后的文件结构


    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 ! 
    ");
    
    if(-z $infile){
        print "
    File is blank!
    ";
        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! 
    ");
    
    open(INFILE, " $infile") || die ("Could not open file $infile ! 
    ");
    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  "
    
    ";
    
                my $full_lstname = $lstname."_lst";  
                print "$dir/$full_lstname
    ";
                
                rename "temp.lst",  "$full_lstname";
                close TEMPA;                #完成一个文件的分割
    
                system"mv $full_lstname lst";
    
                print "
    
    ";
    
                unlink "temp.lst";
                open(TEMPA, ">> temp.lst") || die ("Could not open file temp.lst! 
    ");  #继续进行下一个文件处理
            }
    
            $line =~ /^(w+)[s$W]*/ ;    #正则处理,得到文件名
            print TEMPA  "$line 
    ";
            print "$line 
    ";
            $lstname = $1;
        }
    
        if($line_index == $line_num){       #单独处理最后行情况
            print TEMPA  "
    
    ";
            #print "
    
    ";
    
            my $full_lstname = $lstname."_lst";
            print "$dir/$full_lstname
    ";
            
            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";
    
  • 相关阅读:
    Newtonsoft.Json 把对象转换成json字符串
    分页总页数计算方法 所有分页通用
    好用的Markdown编辑器一览 readme.md 编辑查看
    jquery bootgrid 一个很好的 数据控件,可用于任何语言
    史上最详细“截图”搭建Hexo博客——For Windows
    史上最详细“截图”搭建Hexo博客并部署到Github
    正则表达式30分钟入门教程
    [前端插件]为自己的博客增加打赏功能
    css浮动
    MetaWeblog API调用
  • 原文地址:https://www.cnblogs.com/OneFri/p/6946983.html
Copyright © 2020-2023  润新知