Print to a file instead of to the screen | Member | | Join Date: Sep 2008
Posts: 35
| |
I have the following code that prints to the screen but I want to print the lines to a file that I give as an argument to the script instead... -
#!/usr/bin/perl
-
-
use strict;
-
-
-
####################################################################
-
# constants
-
####################################################################
-
-
use constant COUNT => 0;
-
use constant MAX => 1;
-
use constant MIN => 2;
-
use constant TOTAL => 3;
-
use constant AVERAGE => 4;
-
use constant FCOUNT => 5;
-
use constant FMAX => 6;
-
use constant FMIN => 7;
-
use constant FTOTAL => 8;
-
use constant FAVERAGE => 9;
-
-
####################################################################
-
# genreport
-
####################################################################
-
-
sub genreport {
-
my ($logfile, $reportfile) = @_;
-
my ($line, $elapsed, $facade, %calls);
-
my (@fields, $count, $min, $max, $total, $average,$fcount,$fmin,$fmax,$ftotal,$faverage);
-
my (@list, $totalcalls);
-
my ($falseflag);
-
my ($statdate,$server);
-
my ($starttime,$error,$first);
-
-
open(LOGFILE, "<$logfile")
-
or die "Can't open $logfile: $!";
-
-
open(REPORTFILE, ">$reportfile")
-
or die "Can't open $reportfile: $!";
-
-
$totalcalls = 0;
-
while ($line = <LOGFILE>) {
-
chomp $line;
-
$falseflag= 0;
-
$statdate = substr($line,1,8);
-
$starttime = substr($line,9,12);
-
if($line =~ (/J2CA0027E/))
-
{
-
$falseflag = 1;
-
while ($line =<LOGFILE>){
-
if(($line =~(/_TH/)) || ($line =~ (/invokeMessageEndpointMethod/))) {
-
$line = substr($line,3,length($line));
-
printf "%s %s,%s\n",$statdate,$starttime,$line;
-
last;
-
}
-
}
-
}
-
}
-
}
-
####################################################################
-
# main
-
####################################################################
-
-
if (@ARGV != 2) {
-
die "usage: servercalls.pl <logfile> <reportfile>";
-
}
-
-
my ($logfile, $reportfile) = @ARGV;
-
-
genreport($logfile, $reportfile);
-
-
|  | Site Moderator | | Join Date: May 2007 Location: New Hampshire
Posts: 2,572
| | | re: Print to a file instead of to the screen Quote:
Originally Posted by joeferns79 I have the following code that prints to the screen but I want to print the lines to a file that I give as an argument to the script instead... -
#!/usr/bin/perl
-
-
use strict;
-
-
-
####################################################################
-
# constants
-
####################################################################
-
-
use constant COUNT => 0;
-
use constant MAX => 1;
-
use constant MIN => 2;
-
use constant TOTAL => 3;
-
use constant AVERAGE => 4;
-
use constant FCOUNT => 5;
-
use constant FMAX => 6;
-
use constant FMIN => 7;
-
use constant FTOTAL => 8;
-
use constant FAVERAGE => 9;
-
-
####################################################################
-
# genreport
-
####################################################################
-
-
sub genreport {
-
my ($logfile, $reportfile) = @_;
-
my ($line, $elapsed, $facade, %calls);
-
my (@fields, $count, $min, $max, $total, $average,$fcount,$fmin,$fmax,$ftotal,$faverage);
-
my (@list, $totalcalls);
-
my ($falseflag);
-
my ($statdate,$server);
-
my ($starttime,$error,$first);
-
-
open(LOGFILE, "<$logfile")
-
or die "Can't open $logfile: $!";
-
-
open(REPORTFILE, ">$reportfile")
-
or die "Can't open $reportfile: $!";
-
-
$totalcalls = 0;
-
while ($line = <LOGFILE>) {
-
chomp $line;
-
$falseflag= 0;
-
$statdate = substr($line,1,8);
-
$starttime = substr($line,9,12);
-
if($line =~ (/J2CA0027E/))
-
{
-
$falseflag = 1;
-
while ($line =<LOGFILE>){
-
if(($line =~(/_TH/)) || ($line =~ (/invokeMessageEndpointMethod/))) {
-
$line = substr($line,3,length($line));
-
printf "%s %s,%s\n",$statdate,$starttime,$line;
-
last;
-
}
-
}
-
}
-
}
-
}
-
####################################################################
-
# main
-
####################################################################
-
-
if (@ARGV != 2) {
-
die "usage: servercalls.pl <logfile> <reportfile>";
-
}
-
-
my ($logfile, $reportfile) = @ARGV;
-
-
genreport($logfile, $reportfile);
-
-
The problem I see is that you open the reportfile for writing, but you never reference the filehandle in the print statement. If you want the print statement to go to file, then it would have to look like this: -
printf REPORTFILE "%s %s,%s\n",$statdate,$starttime,$line;
-
Regards,
Jeff
| | Member | | Join Date: Sep 2008
Posts: 35
| | | re: Print to a file instead of to the screen
Thanks, Jeff. Don't know how I missed that.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|