1757426 Members
2284 Online
108860 Solutions
New Discussion юеВ

Re: Perl Script.

 
SOLVED
Go to solution
Chris Tijerina_2
Occasional Advisor

Perl Script.

Hello to all,

I have a perl script that I have written and the functionality works great, but I would like to add to the functionality, make it more automated. How do I in perl pars all files in a directory. Here is the perl script..

#!/usr/bin/perl -w
###############################################################
# summary_parser.pl
# Description:
# This script is intended to parser through a summary report,
# gathering start, end and interval times, to insure that the,
# interval time is within the allowable timeframe. In the event of
# the interval being not acceptable, or the CPU capture ratio being
# unacceptable, this script will email the interested parties.
################################################################
#################### VARIABLES #################################
$ACCEPTABLE_INTERVAL=1000;
$CTR=0;
#$EMAIL_LIST="me\@me.com";
$ERROR_MSG="Data is incomplete";
$SUMMARY="/var/best1/eds_dbc/Oct-14-2001.00.15_eds_dbc.ant";
$LOWER_CAPTURE=.8;
$UPPER_CAPTURE=1.8;
#################### BODY ######################################
#OPEN THE SUMMARY FILE TO READ FROM.
open (SUMRY_FILE,"$SUMMARY") ||
die "Can't Open $SUMMARY";
### READ THE LOGFILE INTO AN ARRAY OF WORDS.
while () {for $chunk(split) {push @ARRAY, $chunk;}}
### COLLECT THE DATA WE NEED.
foreach $x (@ARRAY) {
if ($x eq "Resulting") {
$START_TIME=(substr($ARRAY[$CTR+7], 0,2)*60*60)+(substr($ARRAY[$CTR+7], 3,2)*60)+ (substr($ARRAY[$CTR+7], 6,2));
}
elsif (($x eq "End")&&($ARRAY[$CTR-1] ne "Collect")) {
$END_TIME=(substr($ARRAY[$CTR+5], 0,2)*60*60)+ (substr($ARRAY[$CTR+5], 3,2)*60)+ (substr($ARRAY[$CTR+5], 6,2));
}
elsif ($x eq "Interval") {
$INTERVAL = "$ARRAY[$CTR+3]";
}
elsif ($x eq "Capture") {
$CAPTURE = "$ARRAY[$CTR+2]";
}
$CTR++;
}
### CHECK TO SEE IF THE DATA IS BAD.
if (($START_TIME+$END_TIME - $INTERVAL > $ACCEPTABLE_INTERVAL) || ($CAPTURE < $LOWER_CAPTURE) || ($CAPTURE > $UPPER_CAPTURE)) {
print "\tStart Time: $START_TIME\n+\tEnd Time: $END_TIME\n-\tInterval: $INTERVAL \n=\t\t ".($START_TIME+$END_TIME - $INTERVAL);
print "\n\nCpu Capture Ratio = $CAPTURE\n";
print "\nDATA IS INCOMPLETE FOR $SUMMARY!!!\n";
##NOTIFY THE INTERESTED PARTIES.
$server = `uname -n`;
chomp $server;
$stamp=`date +%y/%m/%d`;
chomp $stamp;
$text = "Data is incomplete";
$msg="$server $stamp $ERROR_MSG";

-----------------------------------------------
Please note the $SUMMARY variable, I am having problems trying pars all files in a directory. I am stumped.

Thanks.

ct
"If you choose not to decide, you still have made a choice."
6 REPLIES 6
Rodney Hills
Honored Contributor
Solution

Re: Perl Script.

opendir(MYDIR,"/path/path/")
while($file=readdir(MYDIR)) {
open(INP," while() {
.. process file ..
}
}
There be dragons...
A. Clay Stephenson
Acclaimed Contributor

Re: Perl Script.

Hi Chris:

The easy way is to make most of what you have eith a subroutine that expect the full pathname as a parameter. Something like this:



$Dirname = "var/best1/eds_dbc";
opendir(D1,$Dirname)
or die "Opening $Dirname $!\n");
my @arry1 = grep /\.ant$/, readdir(D1);
closedir(D1);
chomp(@arry1);
my $i = 0;
while ($i <= $#arry1)
{
$my fullname = sprintf("%s/%s",$Dirname,$arry1[$i]);
printf("Processing %s\n",$fullname);
# call your subroutine here
++$i;
}

I am assuming that .ant is your desired suffix.

That should do it, Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Perl Script.

Hi again. My advanced typing techniques bit me again. This should be the die line:

or die "Opening $Dirname $!\n";

Clay
If it ain't broke, I can fix that.
Chris Tijerina_2
Occasional Advisor

Re: Perl Script.

Clay,

what do you mean by:

printf("Processing %s\n",$fullname);
# call your subroutine here
++$i;
}

"call your subroutine here"??

Thanks,

ct
"If you choose not to decide, you still have made a choice."
A. Clay Stephenson
Acclaimed Contributor

Re: Perl Script.

Hi Chris:

What I meant was that I wanted for you to turn all of your existing script into a perl sub that expects 1 argument - the full pathname to the file that you wish to parse.

e.g.

sub myparser
{
my $SUMMERY = $_[0];
...
...
...
}

You would then call it in the while loop like:


myparser($fullname);

Plan B.

Modify your existing script to parse the command line to get the fullname as the first arg. You then call that perl script from within the perl script containing the loop.

e.g.
my $cmd = sprintf("perl myscript.pl %s",$fullname);
system($cmd);

Plan B is much less efficient!

Regards, Clay


If it ain't broke, I can fix that.
Chris Tijerina_2
Occasional Advisor

Re: Perl Script.

You are too cool! Thanks a million.

If I could assign a million points to you, I would!

ct
"If you choose not to decide, you still have made a choice."