免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1390 | 回复: 2
打印 上一主题 下一主题

帮看下这段perl程序有啥问题? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-06-16 17:03 |只看该作者 |倒序浏览
本帖最后由 zhlong8 于 2012-06-16 20:55 编辑


出现的问题是,目录下有多个文件时存在部分文件仅修改了部分就终止了,是不是不能在同一目录下操作。

sub uncompress {
        my $file = shift;
        #print "1111111111111\n";
        my $new_file = $file;
        $new_file =~ s/\.Z$/\.dat/;
  $new_file =~ s/\.gz$/\.dat/;
        # Uncompressing the file using the system 'zcat' command
        my $rc = system( "zcat '${file}' > '${new_file}'" );
        #print "inDirname:$inDirname\n";
  add_value($inDirname,$new_file);#此处调用子程序
  #print "111111111\n";
        # Checking the return code from the system function
        if( 0 == $rc ) {
                 system("rm '${file}' ");
        }else{
                print "ERROR uncompressing .$file.";
                unlink $new_file;
                return 0;
        }

        return $new_file;
}


sub add_value {#写文件号函数
        my $filepath = shift;        #接收路径
        my $file_name = shift;#接收文件名
        #print "$filepath\n";
        my $file_new =basename($file_name);
  #print "file_new=$file_new";
        opendir (DIR, $filepath) or die "can't open the directory!";
        my @files =grep !/^\./, grep /$file_new/, readdir DIR;
        #print join('!!',@files);
        close DIR;
        foreach my $file (@files) {
                #print $file;
                if ( $file =~/SGSN.*CELLUSR.*\.dat$/ ) {
                       
                        print "$file\n";
                        my $w_file=$file."_tmp";
                        #print $w_file;
                        my @field=split('_',$file);
                        my $len=@field;
                        my $str="";
                        if($len<4){
                                        $str="20120999001";
                        }else{
                                        $str=substr($field[3],0,11);                       
                                }       
                        open (RFILE,"<$filepath/$file")||die"$!\n"; ;
                        open (WFILE,">$filepath/$w_file")||die"$!\n";;
                        while(my $pline=<RFILE>){
                                next if  length(trim($pline))==0;
                                my $newline=trim($pline).'@#$'.trim($str);
                                #print "newline:$newline\n";
                                print WFILE $newline;
                                print WFILE "\n";
                        }
                        close RFILE;
                        close WFILE;
                        #文件重命名
            rename("$filepath/$w_file","$filepath/$file");
                }
        }
}

论坛徽章:
2
CU大牛徽章
日期:2013-04-17 11:46:28CU大牛徽章
日期:2013-04-17 11:46:39
2 [报告]
发表于 2012-06-16 18:10 |只看该作者
貌似看不懂啊,不太科学啊

论坛徽章:
0
3 [报告]
发表于 2012-06-17 22:55 |只看该作者
你说 的错误肯定是不可能的,可以eval 下打印看看
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP