- 论坛徽章:
- 0
|
求助:我下列代码读不到文件内容,请帮忙解答,感谢!!!
###########
## 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; |
|