免费注册 查看新帖 |

Chinaunix

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

求助:读取不到文件内容 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-06-09 06:45 |只看该作者 |倒序浏览
求助:我下列代码读不到文件内容,请帮忙解答,感谢!!!

###########
## process request file
open(REQ_FILE, "$req_file") or die $!;
while (my $line=<REQ_FILE>){
      chomp($line);
      my @fields=split/\|/,$line,-1;
      my $TraceUniqueID = $fields[0];
      my $others = join "|",@fields[1..4];
      $result{$TraceUniqueID} = $others;
      #print "$TraceUniqueID,$result{$TraceUniqueID}\n";
}

## process response file, join with request file
open(RSP_FILE, "$rsp_file") or die $!;
while (my $line=<RSP_FILE>){
      chomp($line);
      my @fields=split/\|/,$line,-1;
      my $TraceUniqueID = $fields[0];
      my $others = join "|",@fields[1..4];
      ## if the request of this uinqueID has been exists, append the respone to it; otherwise, discard this response;
      if (exists($result{$TraceUniqueID})) {
              $result{$TraceUniqueID} = $result{$TraceUniqueID} . "|" . $others ;
      }
      #print "$TraceUniqueID,$result{$TraceUniqueID}\n";
}
###################


全部代码如下:

#!/usr/bin/perl
#

use strict;
use warnings;
use POSIX ":sys_wait_h";

## process start time
my $begin_time = time();
## process end time
my $end_time;
## time consumed
my $final_time;

## get the parameter, format: yyyyMMdd
my $Date_Input = "$ARGV[0]";
## get the year
my $year = substr($Date_Input,0,4);
## get the Month
my $month = substr($Date_Input,4,2);
## get the day
my $day = substr($Date_Input,6,2);
## change the date formate from yyyyMMdd to yyyy-MM-dd
my $Date_Str_2 = "$year-$month-$day";

## timestamp
my $Timestamp = time();
## Path of interface log
my $Interface_Log = "/lun_lcap/SDP_Interface_Log/OSG/log/test";
#my $Interface_Log = "/lun_lcap/SDP_Interface_Log/OSG/log/interface";
## work directory
my $Work_Dir = "/tmp/jaco/scripts";
## directory for output
my $Out_Dir = "/tmp/jaco/scripts/test";
## file name of output
my $Output_FileName = "$Out_Dir/$Date_Str_2.$Timestamp.result";
## request file name
my $req_file = "$Out_Dir/$Date_Str_2.$Timestamp.req";
## responose file name
my $rsp_file = "$Out_Dir/$Date_Str_2.$Timestamp.rsp";
## join request file and response file
my $join_file = "$Out_Dir/$Date_Str_2.$Timestamp.join";
## log files that matched
my $Log = "${Interface_Log}/*_$Date_Input*log";

## maximum number of processes
my $max_proc = 3;
## == number of proc ==
my $num_proc = 0;
## == number of collected ==
my $num_collect = 0;
my $collect;
## == get the child signal, while get the exit signal of a sub process, keep the active process  $num_proc-- ==
$SIG{CHLD} = sub { $num_proc-- };

## files that need to be processed
my @files = glob $Log;
## number of files
my $num_files = @files;
## counter
my $i = 0;
##

## (re)initialize hash
my %result = ();


print "Total $num_files files need to be processed\n";
print "Output file name is: $Output_FileName\n";

## request
open(REQ_FILE, ">$req_file") or die $!;
## response
open(RSP_FILE, ">$rsp_file") or die $!;
## join
open(JOIN_FILE, ">$join_file") or die $!;
## result
open(OUT_FILE, ">$Output_FileName") or die $!;

foreach my $file (@files) {
    ## == fork a new process ==
    my $pid = fork();
    if (!defined($pid)) {
        print "Error in fork: $!";
        exit 1;
    }
    if ($pid == 0) {
        ## == child proc ==
        open(IN_FILE,"$file") or die $!;
        while (my $line=<IN_FILE>){
            chomp($line);
            ## syncSubscriptionData from OSG to SP
            if(($line =~ /\|syncSubscriptionData\|/)&&($line =~ /\|SP\|/)&&($line =~ /$Date_Str_2/)){
                      my @fields=split/\|/,$line,-1;
                      my $TraceUniqueID = substr($fields[7],0,27);
                      my $msisdn = $fields[13];
                      my $spId = $fields[11];
                      my $serviceId = $fields[12];
                      my $interfaceName = $fields[4];
               
               print REQ_FILE ("$TraceUniqueID|$msisdn|$spId|$serviceId|$interfaceName\n");
            }
            ## syncSubscriptionDataResponse from SP to OSG
            elsif (($line =~ /\|syncSubscriptionDataResponse\|/)&&($line =~ /\|SP\|/)&&($line =~ /$Date_Str_2/)){
                    my @fields=split/\|/,$line,-1;
                    my $TraceUniqueID = substr($fields[7],0,27);
                    my $interfaceName = $fields[4];
                    my $logLevel = $fields[1];
                    my $ReturnCode = $fields[9];
                    my $ReturnInfo = $fields[10];
                   
                    print RSP_FILE ("$TraceUniqueID|$interfaceName|$logLevel|$ReturnCode|$ReturnInfo\n");
            }

        }   
     
        close(IN_FILE);
        
        #print " $file : process end\n";
        exit 0;
    }
    $num_proc ++;
    ## == if need to collect zombies ==
    if (($i-$num_proc-$num_collect) > 0) {
        while (($collect = waitpid(-1, WNOHANG)) > 0) {
            $num_collect ++;
        }
    }
    $i++;
   
    while($num_proc > $max_proc){
      ## sleep 1s
      # sleep(1);
      ## sleep 025s
      select(undef, undef, undef, 0.25);
    }
    #do {
    #    sleep(1);
    #} until ($num_proc < $max_proc);
   
}


close(REQ_FILE);
close(RSP_FILE);

## process request file
open(REQ_FILE, "$req_file") or die $!;
while (my $line=<REQ_FILE>){
      chomp($line);
      my @fields=split/\|/,$line,-1;
      my $TraceUniqueID = $fields[0];
      my $others = join "|",@fields[1..4];
      $result{$TraceUniqueID} = $others;
      #print "$TraceUniqueID,$result{$TraceUniqueID}\n";
}

## process response file, join with request file
open(RSP_FILE, "$rsp_file") or die $!;
while (my $line=<RSP_FILE>){
      chomp($line);
      my @fields=split/\|/,$line,-1;
      my $TraceUniqueID = $fields[0];
      my $others = join "|",@fields[1..4];
      ## if the request of this uinqueID has been exists, append the respone to it; otherwise, discard this response;
      if (exists($result{$TraceUniqueID})) {
              $result{$TraceUniqueID} = $result{$TraceUniqueID} . "|" . $others ;
      }
      #print "$TraceUniqueID,$result{$TraceUniqueID}\n";
}

close(REQ_FILE);
close(RSP_FILE);

## print the join result to the file
while (my ($TraceUniqueID,$others)=each(%result)) {
        print JOIN_FILE ("$TraceUniqueID|$others\n");
}
close(JOIN_FILE);

##

#open(JOIN_FILE, "$join_file") or die $!;

close(OUT_FILE);

## count the time consumed
$end_time = time();
$final_time = $end_time - $begin_time;  
print "Total: $final_time seconds consumed.\n";

exit 0;

论坛徽章:
0
2 [报告]
发表于 2012-06-09 09:33 |只看该作者
至少说说,代码总体的意图吧,太长了。
读不到文件,一个原因是文件是否存在,另一个原因是,是否有权限,再一个就是逻辑上的处理是否有问题。

论坛徽章:
0
3 [报告]
发表于 2012-06-09 09:40 |只看该作者
mysqllog 发表于 2012-06-09 09:33
至少说说,代码总体的意图吧,太长了。
读不到文件,一个原因是文件是否存在,另一个原因是,是否有权限, ...


//
代码用来处理几万个文件,从中按条件抽取符合条件的数据,输入到2个文件中;

完了后,对这2个文件进行处理,代码中来读这2个文件的时候,发现没有输出,程序运行完后,明显看到上面2个文件是产生了的。

论坛徽章:
2
CU大牛徽章
日期:2013-04-17 11:46:28CU大牛徽章
日期:2013-04-17 11:46:39
4 [报告]
发表于 2012-06-09 09:43 |只看该作者
代码不是您自己写的么?那两个文件有内容么?

论坛徽章:
0
5 [报告]
发表于 2012-06-09 09:49 |只看该作者
cdtits 发表于 2012-06-09 09:43
代码不是您自己写的么?那两个文件有内容么?


文件是有内容的。

论坛徽章:
0
6 [报告]
发表于 2012-06-09 09:54 |只看该作者
不晓得文件绝对路径和文件格式(加上后缀)对LZ有木有帮助!

注释挪到代码后面,是不是可读性更强?!

可否考虑用子程序?:wink:

论坛徽章:
0
7 [报告]
发表于 2012-06-09 10:28 |只看该作者
tempo8 发表于 2012-06-09 09:54
不晓得文件绝对路径和文件格式(加上后缀)对LZ有木有帮助!

注释挪到代码后面,是不是可读性更强?!


先帮忙解决问题,谢谢!

论坛徽章:
2
CU大牛徽章
日期:2013-04-17 11:46:28CU大牛徽章
日期:2013-04-17 11:46:39
8 [报告]
发表于 2012-06-09 10:47 |只看该作者
你的内容在 %result 中,然后写到 $join_file 中,$out_file 中没有写入内容。

谁也不知到你有什么问题?while 循环中读的每一行打印出来看看,print "$line\n";

论坛徽章:
0
9 [报告]
发表于 2012-06-09 11:42 |只看该作者
cdtits 发表于 2012-06-09 10:47
你的内容在 %result 中,然后写到 $join_file 中,$out_file 中没有写入内容。

谁也不知到你有什么问题? ...


问题在于读文件的时候,那2个文件的内容还是空的;但是程序运行完后,文件内容有了;是不是当时内容还没写入?没有道理啊

论坛徽章:
2
CU大牛徽章
日期:2013-04-17 11:46:28CU大牛徽章
日期:2013-04-17 11:46:39
10 [报告]
发表于 2012-06-09 11:48 |只看该作者
open 后加上如下试试:

  1. select((select(REQ_FILE), $|=1)[0]);
  2. select((select(RSP_FILE), $|=1)[0]);
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP