zoukankan      html  css  js  c++  java
  • perl合并文件

    使用Perl合并文件

    有时需要将整个目录下的小文件合并到一个文件中,以便查阅检索

    特性


    整个目录完全遍历,自动存入单个文件
    顺序遍历文件

    待合并的目录


    合并后的文件内容


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

    Perl代码


     
    #!/usr/bin/perl
    use warnings;
    
    ###########################################
    #./mergelst.pl lst
    ###########################################
    
    my $indir = $ARGV[0];
    
    opendir DIR, $indir or die "Connot open $indir: $!";    #得到目录句柄
    
    unlink "temp.lst";
    open(OUTFILE, ">>temp.lst") || die ("Could not open file temp.lst ! 
    ");  #输出文件句柄
    
    foreach my $file (sort readdir DIR){                    #目录下文件排序后遍历文件
        if($file =~ /^./){
            next;                                           #'.''..'目录去除,不包括在遍历的范围之内
        }
        print "$indir$file
    ";
    
        open(INFILE, "$indir/$file") || die ("Could not open file $file ! 
    "); #打开目录下的文件
          while ($line = <INFILE>){                                             #循环输出到输出文件中,合并
              chomp($line);
              print OUTFILE  "$line 
    ";
          }
        print OUTFILE  "
    
    ";
        close INFILE;
    }
    
    close OUTFILE;
    rename 'temp.lst','merge_lst';                 #文件重名名
    print  "
     merge_lst
    
    ";
    from:乔_木
  • 相关阅读:
    Leetcode 349. Intersection of Two Arrays
    hdu 1016 Prime Ring Problem
    map 树木品种
    油田合并
    函数学习
    Leetcode 103. Binary Tree Zigzag Level Order Traversal
    Leetcode 102. Binary Tree Level Order Traversal
    Leetcode 101. Symmetric Tree
    poj 2524 Ubiquitous Religions(宗教信仰)
    pat 1009. 说反话 (20)
  • 原文地址:https://www.cnblogs.com/YLuluuu/p/9009025.html
Copyright © 2011-2022 走看看