zoukankan      html  css  js  c++  java
  • Perl 文件处理范例

    觉得这个范例不错就保存了,原文地址在这里:http://www.cnblogs.com/zhangzhi/archive/2010/10/19/1855302.html

    Perl 文件处理范例

    . 任意字符

    ?0或者1个

    * 任意个

    + 一个或者以上

    $_ 默认数组

    $@ 第一被匹配的字符

    $` 被匹配字符之前的字符

    $' 被匹配字符之后的字符

    $1 第一个被匹配的字符,以左括号的顺序算。

    <>砖石输入符

    =~ 匹配判断符号

    d 数字

    D 非数字

    w [A-Za-z0-9_]

    W 非  [A-Za-z0-9_]

    s 字符

    S 非字符

     {n} 重复n次

    open FILE, "file.txt" 打开已经存在的文件

    open FILE,">file.txt" 打开file.txt,如果不存在的话就创建file.txt

    open FILE,">>file.txt" 打开,并将新内容追加到文本的末端,如果不存在的话,创建file.txt

     

    双引号内的转义符

               换行
                回车
                制表符
    f             formfeed
              退格
    a         响铃
    e          escape(ASCII 中的escape 字符)
    07      任何八进制值(这里是,007=bell(响铃))
    x7f       任何十六进制值(这里是,007=bell)
    cC          一个控制符(这里是,ctrl +c)
    \           反斜线
    ”        双引号
    l          下个字符小写
    L        接着的字符均小写直到E
    u        下个字符大写
    U          接着的字符均大写直到E
    Q        在non-word 字符前加上,直到E
    E         结束L,U 和Q
     

    example1

    从一个mail.list中识别 @eric.com的usr name,排序后输出到result.list中。

    mail.list:

    f@brand.com
    d@eric.com
    g@syv.com
    a@eric.com
    h@mail.com
    c@eric.com
    x@joey.com
    b@eric.com

    perl script:

    复制代码
    代码
    #! /usr/bin/perl   #perl directory declaration

    open MAIL,"mail.list";               #open and read mail.list context
    open RESULT,">result.list";       #create a new file named" result.list", using filehandle "RESULT" to transfer information

    $n =0;                                   #define a varibale


    foreach (<MAIL>){                  #processing each line from context <MAIL>
    if(/(@eric.com)/)                   # judge if pattern"@eric.com" matched, and store it in "$&"  
        {   $array[$n]= "$` ";        # store words before matched("$`") in @array
            $n =$n +1;                    #index add one
        }
    }

    @sorted = sort (@array);          # sort array by letters

    print RESULT @sorted;              # print array in output file

    close RESULT;                         # close file
    close MAIL;
    复制代码

    result.list

    a
    b
    c
    d

     example2

    将当前目录下所有 .cc结尾的文件 重命名为 .c结尾

    复制代码
    #! /usr/bin/perl

    @list =glob('./*.cc');

    foreach $list(@list){
        my $name = $list;
        $name =~ s/cc$/c/;
        rename $list,$name;  
        }
    复制代码

     example 3

    将文件的中的各个module 实例化,输出到新的文件中。

    复制代码
    代码
    #! /usr/bin/perl


    open TMP,">instance.v";

    while(<>){
    if(/^module (.*)((.*));$/m){
        $module_name =$1;
        $port_list = $2;
        
        @ports = split(/,/,$port_list);
        my $n =@ports;
        my $i=0;

        print TMP "$module_name U_$module_name( ";

        for($i=0; $i<$n; $i =$i+1){
            print TMP "$ports[$i]($ports[$i]), ";
            if($i==$n -1){
             print TMP "); ";
            }
        }   
    }      
    }


    close TMP;
    close CODE;
    复制代码

     windows下perl脚本范例

    复制代码
     1 #! C:strawberryperlinperl
     2 system("update.bat"); #运行脚本
     3 open FILE, "file.lst";
     4 @lines =<FILE>;#将文本所有内容读入@lines
     5 
     6 foreach $lines (@lines){ #处理@lines中的每行
     7     chomp($lines);
     8     open SOURCE,"$lines";
     9     my @content=<SOURCE>;
    10     open Result ,"> ./result/$lines"; #windows下的路径也是用斜杠,而不是反斜杠
    11     #$lines =~ s/txt/png/;
    12     #print Result "$lines ";
    13     foreach $content(@content)
    14     {
    15         if($content =~ /Image filename/)
    16         {    
    17             chomp($content);
    18             @dirname =split(/"/,$content);
    19             print Result "$dirname[$#dirname]  ";    
    20             print "$dirname[$#dirname]  ";
    21         }
    22 
    23 
    24         if($content =~ /(Objects with ground truth : )/)
    25         {
    26             $re = $';
    27             @num =split(/s/,$re);
    28             print Result "$num[0]  ";    
    29         }        
    30 
    31         if($content =~ /((Xmin, Ymin) - (Xmax, Ymax) :)/)
    32         {
    33             $a=$';
    34             if($a =~ /([0-9]+)D*([0-9]+)D*([0-9]+)D*([0-9]+)/)
    35             {
    36                 print Result "$1 $2 $3 $4  ";
    37             }
    38     
    39         }
    40 
    41     }
    42         close SOURCE;
    43         close Result;
    44 
    45 }
    46 close FILE;
    47 
    48 
    复制代码
  • 相关阅读:
    生成函数初步
    Lg 8月赛(构造+交互)
    wqs 二分学习笔记
    FastAPI 学习之路(十六)Form表单
    线性代数入门
    Oracle-PDB拔插
    MySQL-audit审计插件
    MySQL-用户与权限管理
    MySQL-存储引擎
    MySQL-逻辑结构
  • 原文地址:https://www.cnblogs.com/lize19940412/p/6635756.html
Copyright © 2011-2022 走看看