By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,660 Members | 1,299 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,660 IT Pros & Developers. It's quick & easy.

Print to a file instead of to the screen

P: 37
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.  
Sep 2 '09 #1
Share this Question
Share on Google+
2 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
@joeferns79
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
Sep 2 '09 #2

P: 37
Thanks, Jeff. Don't know how I missed that.
Sep 3 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.