2012-07-10 17 views
-1

我傳遞所述輸入文件的位置和輸出的文件位置通過命令行和我收到這些錯誤:值無效對於選項輸入(編號預期)

值「C:\用戶\ Mruppe2 \工作區\ Perl的C」爲選項輸入 (數預期)
值無效 「:\ TEMP \ output.csv」 爲無效選項輸出 (預期擴展號碼)

在5MinInterval2.pl線154沒有這樣的文件或目錄。

命令行輸入看起來像這樣:

perl的5MinInterval2.pl -i C:\用戶\ Mruppe2 \工作空間\ Perl的5基本 訓練\ MPMStats項目\ error_log中-o C:\ TEMP \輸出的.csv

這裏是我的代碼:

# This program parses a error_log for necessary information and outputs a CSV file with the highest busy value for each 5 minute interval. 

use strict; 
use warnings; 
use Getopt::Long; 

#Define argument types 
my $input = ''; 
my $output = ''; 
GetOptions('input=i' => \$input,'output=o' => \$output); 

# Ignore theses values 
my %ignorables = map { $_ => 1 } qw([notice mpmstats: rdy bsy rd wr ka log dns cls bsy: in); 

# Subroutine to pull the line containing AP22,SM22, and Apache stats 
sub findLines { 
    my($item,@result)=(""); 

    # Iterates over the lines in the file, putting each into $_ 
    while ($input) {  

     # Select only those fields that have the word 'notice' 
     if (/\[notice/) {   

      # Place those lines with the word 'rdy' on the next line 
      if (/\brdy\b/){ 
       push @result,"$item\n"; 
       $item=""; 

      } 
      else { 
       $item.=","; 
      } 

      # Split the line into fields, separated by spaces, skip the %ignorables   
      my @line = grep { not defined $ignorables{$_} } split /\s+/;  

      # More cleanup   
      s/|^\[|notice|[]]//g for @line; # remove unnecessary elements from the array 

      # Output the line. 
      @line = join(",", @line);   
      s/,,/,/g for @line; 
      map $item.=$_, @line; 
      } 
     } 
     @result 
    } 

# Place the subroutine contents into an array 
my @array = &findLines; 
my $line; 

# Create an subroutine to place the contents of AP22, SM22, and Apache in the correct order 
sub Program{ 
    my @return =(); 
    chomp @array; 

    my ($dow,$mon,$day,$time,$year,$rdy,$bsy,$rd,$wr,$ka,$log,$dns,$cls,$dow2,$mon2,$day2,$time2,$year2,$val1,$mod1,$val2,$mod2,$val3,$mod3,$ap22,$sm22,$apache); 
    foreach $line (@array){ 
    ($dow,$mon,$day,$time,$year,$rdy,$bsy,$rd,$wr,$ka,$log,$dns,$cls,$dow2,$mon2,$day2,$time2,$year2,$val1,$mod1,$val2,$mod2,$val3,$mod3) = ((split /[,]/, $line),("")x24); 
    $line = "$dow,$mon,$day,$time,$year,$rdy,$bsy,$rd,$wr,$ka,$log,$dns,$cls"; 

    # For lines with no variables 
    if ($mod1 eq ""){ 
     $line = $line.","."0".","."0".","."0"; 
    } 

    # For lines with only SM22 
    if ($mod1 eq "mod_sm22.cpp" && $mod2 eq ""){ 
     $line = $line.",".$val1.","."0".","."0"; 
    } 

    # For lines with only AP22 
    if ($mod1 eq "mod_was_ap22_http.c" && $mod2 eq "" && $mod3 eq ""){ 
     $line = $line.",".$val1.","."0".","."0"; 
    } 

    # For lines with AP22-SM22-Apache 
    if ($mod1 eq "mod_was_ap22_http.c" && $mod2 eq "mod_sm22.cpp" && $mod3 eq "ApacheModule.cpp"){ 
     $line = $line.",".$val1.",".$val2.",".$val3; 
    } 

    # For lines with SM22-AP22-Apache 
    if ($mod1 eq "mod_sm22.cpp" && $mod2 eq "mod_was_ap22_http.c" && $mod3 eq "ApacheModule.cpp"){ 
     $line = $line.",".$val2.",".$val1.",".$val3; 
    } 

    # For lines with AP22-SM22 
    if ($mod1 eq "mod_was_ap22_http.c" && $mod2 eq "mod_sm22.cpp" && $mod3 eq ""){ 
     $line = $line.",".$val1.",".$val2.","."0"; 
    } 

    # For lines with SM22-AP22 
    if ($mod1 eq "mod_sm22.cpp" && $mod2 eq "mod_was_ap22_http.c" && $mod3 eq ""){ 
     $line = $line.",".$val2.",".$val1.","."0"; 
    } 

    # For lines with SM22-Apache 
    if ($mod1 eq "mod_sm22.cpp" && $mod2 eq "ApacheModule.cpp" && $mod3 eq ""){ 
     $line = $line.","."0".",".$val2.",".$val2; 
    } 

    # For lines with AP22-Apache 
    if ($mod1 eq "mod_was_ap22_http.c" && $mod2 eq "ApacheModule.cpp" && $mod3 eq ""){ 
     $line = $line.",".$val1.","."0".",".$val2; 
    } 

    # Push the array out of the subroutine 
    ($dow,$mon,$day,$time,$year,$rdy,$bsy,$rd,$wr,$ka,$log,$dns,$cls,$ap22,$sm22,$apache) = ((split/[,]/, $line),("")x16); 
    push @return, ("$line\n"); 
    } 
    return @return; 
} 

# Initialize the hashes 
my %interval; 
my %month; @month{qw/ jan feb mar apr may jun jul aug sep oct nov dec /} = '01' .. '12'; 

# Put the contents of the subroutine into an array 
my @finalArray = &Program; 

# Delete the first line of the array 
$finalArray[0] = ""; 

# Create a new array without the first line 
my @lastArray = @finalArray; 


# Select only those lines with the highest busy count in each 5 minute interval 
my @maxima; 
for my $record (@lastArray) {  
    my @fields = $record =~ /([^,\s]+)/g; 
    next unless @fields;  
    my @range = @fields[1..4]; 
    $range[2] =~ s|(\d+):\d\d$|5*int($1/5)|e; 
    my $range = join ' ', @range; 
    my $value = $fields[5]; 
    my ($dow, $mon, $day, $time, $year, $rdy, $by, $rd, $wr, $ka, $log, $dns, $cls, $ap22, $sm22, $apache) = split /[,]/, $record; 
    my $record2 = "$range,$rdy,$by,$rd,$wr,$ka,$log,$dns,$cls,$ap22,$sm22,$apache"; 
    if (@maxima == 0 or $range ne $maxima[-1][0]) {  
     push @maxima, [$range, $value, $record2]; 
    } 
    else {  
     @{$maxima[-1]}[1,2] = ($value, $record2) if $maxima[-1][1] > $value; 
    } 
} 

# Print the contents to the CSV file 
open my $fh, ">", $output or die $!; 
print $fh "Time Interval,rdy,bsy,rd,wr,ka,log,dns,cls,ap22,sm22,apache\n"; 
print $fh $_->[2] for @maxima; 

我在做什麼錯?

回答

2

嗯,你沒宣佈「輸入」是一個整數,通過它的外觀:

... 'input=i' ... 

也許你要找的字符串:

... 'input=s' .... 
+0

然後我怎麼把它從命令行如果我有2個字符串? – rupes0610 2012-07-10 19:10:47

+1

@ user1488984您可以使用長名稱,或者查看[this](http://search.cpan.org/~jv/Getopt-Long-2.38/lib/Getopt/Long.pm#Options_with_multiple_names)您沒有閱讀文檔? – TLP 2012-07-10 19:19:28