CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
loop log files

 



taylorhawkins
New User

Oct 24, 2010, 10:38 AM

Post #1 of 2 (242 views)
loop log files Can't Post

Somehow I took on the responsibility of creating a Perl script at work to read log files. The basics is that it needs to read a text file filled with scanner information and give me the read/noread percentage of those scanners.
I actually have it working fine! Now i'm impressed with Perl but can't seem to find anything on Google or this forum to help me clean the code up.

I basically brute forced through the logic with a lot of copy pasting and want to clean it up with looping if possible.

Mycode (abbreviated with ...'s)

Code
open(MYINPUTFILE, "<LogicAuditLog-20101018.txt") || die("Could not open file"); 
open(RESULTS, '>Results.txt') || die("Couldn't find Results.txt in same directory");
while(<MYINPUTFILE>)
{
my($line) = $_;
chomp($line);#cut newlines

$line =~ tr/[a-z]/[A-Z]/;#everything upper case

if($line =~ m/(SC13)/)
{
$S13++;
if($line =~ m/(NOREAD)/){$N13++;}
}
if($line =~ m/(SC15)/)
{
$S15++;
if($line =~ m/(NOREAD)/){$N15++;}

}
. . . . . .
$P13 = sprintf '%.2f%%', 100 * $N13 / $S13;
$P15 = sprintf '%.2f%%', 100 * $N15 / $S15;
$P17 = sprintf '%.2f%%', 100 * $N17 / $S17;
. . . . . .
print RESULTS "Scanner 13 reads: $S13 noreads: $N13 percentage: $P13 \n";
print RESULTS "Scanner 15 reads: $S15 noreads: $N15 percentage: $P15 \n";
print RESULTS "Scanner 17 reads: $S17 noreads: $N17 percentage: $P17 \n";


I have been trying something like this but haven't been able to get it to work.


Code
$S13=13;$S14=14;$S17=17;$S18=18;$S22=22;$S23=23;$S24=24;$S25=25;$S26=26;$S29=29;$S39=39; 
$S42=42;$S45=45;$S50=50;$S53=53;$S56=56;$S60=60;$S64=64;$S65=65;$S66=66;$S67=67;

@scan_array=($S13,$S14,$S17,$S18,$S22,$S23,$S24,$S25,$S26,$S29,$S39,$S42,$S45,$S50,$S53,$S56,$S60,$S64,$S65,$S66,$S67);
$size = @scan_array;


for($i=0; $i<$size; $i++)
{
if($line =~ m/$scan_array[$i]/)
{
print $scan_array[$i];
$sc = $scan_array[$i];
$sc++
#print $scan_array;
#if($line =~ m/(NOREAD)/){$N13++;}
}
}


The log it reads looks like this but thousands of lines. I need the lines that say SORT and either a read or no read.


Code
05:39:47.569  SC22                    75 Sort            900000122                      False        PTL-1                                       0.050       0.066       0.000      0.116      0.787      01900000122                                                  1 - OK                         
05:39:49.085 SC22 75 Confirm 900000122 False PTL-1 0.000 0.000 0.292 0.000 0.000 1 - OK
05:39:49.132 SC23 93 Confirm 900000083 False PTL-3 0.000 0.000 0.281 0.000 0.000 1 - OK
05:39:49.694 SC23 94 Sort 900000130 False PTL-4 0.017 0.007 0.000 0.024 0.186 02900000130 1 - OK
05:39:51.538 SC23 94 Confirm 900000130 False PTL-4 0.000 0.000 0.131 0.000 0.000 1 - OK
05:39:51.679 SC22 76 Sort NOREAD True Straight 0.010 0.001 0.000 0.011 0.824 01NOREAD 124 - No-Read container.


thanks for any help in advance!


rovf
Veteran

Oct 27, 2010, 5:40 AM

Post #2 of 2 (227 views)
Re: [taylorhawkins] loop log files [In reply to] Can't Post

Wouldn't a hash help here, so that you have something like:

my %S=();
my %N=();
...
if($line =~ m/SC([\d+])/)
{
my $number=$1;
$S{$number}++;
if($line =~ m/(NOREAD)/){$N{$number}++;}
}

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives