Connecting Tech Pros Worldwide Forums | Help | Site Map

Print to a file instead of to the screen

Member
 
Join Date: Sep 2008
Posts: 35
#1: Sep 2 '09
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...

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4.  
  5.  
  6. ####################################################################
  7. # constants
  8. ####################################################################
  9.  
  10. use constant COUNT   => 0;
  11. use constant MAX     => 1;
  12. use constant MIN     => 2;
  13. use constant TOTAL   => 3;
  14. use constant AVERAGE => 4;
  15. use constant FCOUNT   => 5;
  16. use constant FMAX   => 6;
  17. use constant FMIN   => 7;
  18. use constant FTOTAL  => 8;
  19. use constant FAVERAGE   => 9;
  20.  
  21. ####################################################################
  22. # genreport
  23. ####################################################################
  24.  
  25. sub genreport {
  26.    my ($logfile, $reportfile) = @_;
  27.    my ($line, $elapsed, $facade, %calls);
  28.    my (@fields, $count, $min, $max, $total, $average,$fcount,$fmin,$fmax,$ftotal,$faverage);
  29.    my (@list, $totalcalls);
  30.    my ($falseflag);
  31.    my ($statdate,$server);
  32.    my ($starttime,$error,$first);
  33.  
  34.    open(LOGFILE, "<$logfile")
  35.       or die "Can't open $logfile: $!";
  36.  
  37.    open(REPORTFILE, ">$reportfile")
  38.       or die "Can't open $reportfile: $!";
  39.  
  40.    $totalcalls = 0;
  41.    while ($line = <LOGFILE>) {
  42.       chomp $line;
  43.       $falseflag= 0;
  44.       $statdate = substr($line,1,8);
  45.       $starttime = substr($line,9,12);
  46.       if($line =~ (/J2CA0027E/))
  47.        {
  48.        $falseflag = 1;
  49.        while ($line =<LOGFILE>){
  50.         if(($line =~(/_TH/)) || ($line =~ (/invokeMessageEndpointMethod/))) {
  51.          $line = substr($line,3,length($line));
  52.          printf "%s %s,%s\n",$statdate,$starttime,$line;
  53.          last;
  54.         }
  55.       }
  56.     }
  57.  }
  58. }
  59. ####################################################################
  60. # main
  61. ####################################################################
  62.  
  63. if (@ARGV != 2) {
  64.    die "usage: servercalls.pl <logfile> <reportfile>";
  65. }
  66.  
  67. my ($logfile, $reportfile) = @ARGV;
  68.  
  69. genreport($logfile, $reportfile);
  70.  
  71.  

numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,572
#2: Sep 3 '09

re: Print to a file instead of to the screen


Quote:

Originally Posted by joeferns79 View Post

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...

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4.  
  5.  
  6. ####################################################################
  7. # constants
  8. ####################################################################
  9.  
  10. use constant COUNT   => 0;
  11. use constant MAX     => 1;
  12. use constant MIN     => 2;
  13. use constant TOTAL   => 3;
  14. use constant AVERAGE => 4;
  15. use constant FCOUNT   => 5;
  16. use constant FMAX   => 6;
  17. use constant FMIN   => 7;
  18. use constant FTOTAL  => 8;
  19. use constant FAVERAGE   => 9;
  20.  
  21. ####################################################################
  22. # genreport
  23. ####################################################################
  24.  
  25. sub genreport {
  26.    my ($logfile, $reportfile) = @_;
  27.    my ($line, $elapsed, $facade, %calls);
  28.    my (@fields, $count, $min, $max, $total, $average,$fcount,$fmin,$fmax,$ftotal,$faverage);
  29.    my (@list, $totalcalls);
  30.    my ($falseflag);
  31.    my ($statdate,$server);
  32.    my ($starttime,$error,$first);
  33.  
  34.    open(LOGFILE, "<$logfile")
  35.       or die "Can't open $logfile: $!";
  36.  
  37.    open(REPORTFILE, ">$reportfile")
  38.       or die "Can't open $reportfile: $!";
  39.  
  40.    $totalcalls = 0;
  41.    while ($line = <LOGFILE>) {
  42.       chomp $line;
  43.       $falseflag= 0;
  44.       $statdate = substr($line,1,8);
  45.       $starttime = substr($line,9,12);
  46.       if($line =~ (/J2CA0027E/))
  47.        {
  48.        $falseflag = 1;
  49.        while ($line =<LOGFILE>){
  50.         if(($line =~(/_TH/)) || ($line =~ (/invokeMessageEndpointMethod/))) {
  51.          $line = substr($line,3,length($line));
  52.          printf "%s %s,%s\n",$statdate,$starttime,$line;
  53.          last;
  54.         }
  55.       }
  56.     }
  57.  }
  58. }
  59. ####################################################################
  60. # main
  61. ####################################################################
  62.  
  63. if (@ARGV != 2) {
  64.    die "usage: servercalls.pl <logfile> <reportfile>";
  65. }
  66.  
  67. my ($logfile, $reportfile) = @ARGV;
  68.  
  69. genreport($logfile, $reportfile);
  70.  
  71.  

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:

Expand|Select|Wrap|Line Numbers
  1. printf REPORTFILE "%s %s,%s\n",$statdate,$starttime,$line;
  2.  
Regards,

Jeff
Member
 
Join Date: Sep 2008
Posts: 35
#3: Sep 3 '09

re: Print to a file instead of to the screen


Thanks, Jeff. Don't know how I missed that.
Reply