422,530 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,530 IT Pros & Developers. It's quick & easy.

perl script will generate html file, converting this to excel is not happening helpme

P: 2
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use XML::Simple;
  4. use Data::Dumper;
  5. use File::stat;
  6. use Time::localtime;
  7. use POSIX ();
  8. use File::Basename;
  9. use File::Path qw(mkpath);
  10.  
  11. #use Env;
  12.  
  13. print "Content-type:text/html\r\n\r\n";
  14.  
  15. my $sourceName = $ARGV[0];
  16.  
  17. my $cobdate;
  18.  
  19. my $TMPFILE;
  20.  
  21. my $TMP_FILE;
  22.  
  23. my $directory ;
  24.  
  25. my $riskError;
  26.  
  27. my $tradeError;
  28.  
  29. my $cobdateTag;
  30.  
  31. my $fileTypeTag;
  32.  
  33. my $rmErrorBodyTag;
  34.  
  35. my $bookIdTag;
  36.  
  37. my $subBookIdTag;
  38.  
  39. my $gbmBookRefTag;
  40.  
  41. my $bookTag;
  42.  
  43. my $ticketNumber;
  44.  
  45. my $riskTypeTag;
  46.  
  47. my $errorDescriptionTag;
  48.  
  49. my $errorTypeTag;
  50.  
  51. my $reg;
  52.  
  53. my( @logLines );
  54.  
  55. my $zipfilename;
  56.  
  57. my $sourceSysIdTag;
  58.  
  59. my $runLogFhnd;
  60.  
  61. my $surfaceError;
  62.  
  63. my $timeStamp;
  64.  
  65. sub LDATA() { "DATA"; }
  66.  
  67. sub LDEBUG(){ "DEBUG"; }
  68.  
  69. sub LINFO() { "INFO"; }
  70.  
  71. sub LWARN() { "WARN"; }
  72.  
  73. sub LERROR(){ "ERROR"; }
  74.  
  75. sub LFATAL(){ "FATAL"; }
  76.  
  77. sub LSTATS(){ "STATS"; }
  78.  
  79. sub myTRUE() { 1; }
  80. sub myFALSE() { 0; }
  81.  
  82. sub mySUCCESS() { 0; }
  83.  
  84. sub myFAIL() { 2; }
  85.  
  86.  
  87. ################################################################################
  88. # Logging functions.
  89.  
  90. # Default is logging to STDOUT, but will also log to logfile if file handle
  91.  
  92. ################################################################################
  93.  
  94.  
  95. sub logLine($$)
  96. {
  97.     my( $level, $line ) = @_;
  98.         my( $curTs ) = getCurrentDateTimeStr();
  99.  
  100.     if( $level eq LDEBUG )
  101.     {
  102.  
  103.             printf( "%-5s: %s\n", $level, $line );
  104.             if(defined($runLogFhnd))
  105.             {
  106.                 my($line) = sprintf( "$$:%-5s: %s\n", $level, $line );
  107.                 print $runLogFhnd $line;
  108.             }
  109.  
  110.         }
  111.     else
  112.     {
  113.         printf( "%-5s: %s\n", $level, $line );
  114.         if(defined($runLogFhnd))
  115.         {
  116.             my($line) = sprintf( "$$:%-5s: $curTs %s\n", $level, $line );
  117.             print $runLogFhnd $line;
  118.         }
  119.     }
  120.  
  121.     if($level eq LFATAL)
  122.     {
  123.                 my($package, $filename, $line) = caller;
  124.        # printf("%5s: $curTs : Aborting due to FATAL error in '$package' around line No. : $line\n", $level);
  125.                 if(defined($runLogFhnd))
  126.                 {
  127.         #               my($line) = sprintf("$$:%5s: $curTs : Aborting due to FATAL error in '$package' around line No. : $line\n", $level);
  128.                         print $runLogFhnd $line;
  129.                         close($runLogFhnd);
  130.                 }
  131.         exit 2;
  132.     }
  133. }
  134.  
  135. ################################################################################
  136. # sub getCurrentDateTimeStr
  137. ################################################################################
  138. sub getCurrentDateTimeStr()
  139. {
  140.         my ($sec,$min,$hour,$day,$month,$yr19,@rest) = localtime();
  141.         return($timeStamp);
  142. }
  143.  
  144. ################################################################################
  145. # sub checkAndCreateDirPath
  146. # Go through the full dirpath..
  147. ################################################################################
  148. sub checkAndCreateDirPath($)
  149. {
  150.         my $dirpath = shift;
  151.  
  152.         unless ( -d "$dirpath" )
  153.         {
  154.                 logLine(LINFO, "checkAndCreateDirPath: Creating Dir : $dirpath");
  155.                 eval { mkpath($dirpath) };
  156.                 if ($@) 
  157.                 {
  158.                         logLine(LFATAL, "checkAndCreateDirPath: Failed to create '$dirpath' : $@");
  159.                 }
  160.                 else
  161.                 {
  162.                         logLine(LINFO, "checkAndCreateDirPath: Created  Dir : $dirpath");
  163.                 }
  164.  
  165.         }
  166. }
  167.  
  168.  
  169.  
  170. ################################################################################
  171. ### sub checkAndCreateDirPath
  172. ### Go through the full dirpath..
  173. ##################################################################################
  174.  
  175.  
  176. sub CreateBackupFile($)
  177. {
  178.    my $tmpFile=shift;
  179.  
  180.  
  181.  
  182.       if(-f "$tmpFile")
  183.          {
  184.                print "TMP file already exists";
  185.  
  186.                   }else{
  187.  
  188.                         print "creating a file";
  189.  
  190.                                   open my $OUT, '>>', $tmpFile or die "can't create tmp file '$tmpFile' : $!";
  191.                                      }
  192.                                      my $USER_PWD='m_o/Abcdefa321hdrgf@K_M_A';
  193.  
  194.                                                my $previouscob=`sqlplus -s $USER_PWD<< EOF
  195.                                                set heading off;
  196.                                                set pagesize 0;
  197.                                                select to_char(T_MINUS_1_DATE,'YYYYMMDD') from  MYRIAD_SYSTEM_DATE;
  198.                                                EOF`;
  199.  
  200.  
  201.                                                $previouscob =~ s/\s+$//;
  202.  
  203.                                                print "$previouscob";
  204.  
  205.                                                my $prev_file="/var/tmp/checkFilePresent_$previouscob.csv";
  206.                                                if (-f "$prev_file"){
  207.                                                unlink $prev_file;
  208.                                                print "removed previos cobdate files";
  209.                                                }
  210.  
  211.                                                }
  212.  
  213.  
  214. ##############################################
  215.  
  216. # my $myriad_env_filepath = $ENV{`MYRIAD_HOME/common/conf/myriad.env`};
  217. #my $myriad_env_filepath = {'/cs/rfcs/myriad/envs/PRD/common/conf/myriad.env'};
  218.  
  219. # my $USER_PWD = $ENV{`MYRIAD_DB_CONN`};
  220.  
  221. #my $USER_PWD='myriad_user/user_myriad1234@PLNMY010_MYRIAD_APP';
  222. #my $USER_PWD='myriad_user/jHgdbfUhfzrhMF82@PZHMY010';
  223. my $USER_PWD='myriad_owner/Abcdefa321hdrgf@KZHMY103_MYRIAD_APP';
  224.  
  225.  $cobdate =`sqlplus -s $USER_PWD<< EOF
  226. set heading off;
  227. set pagesize 0;
  228. select to_char(T_ZERO_DATE,'YYYYMMDD') from  MYRIAD_SYSTEM_DATE;
  229. EOF`;
  230.  
  231. $cobdate =~ s/\s+$//;
  232.  
  233. if ($sourceName eq "PrimoRisk") {
  234.           $directory = "/cs/myrow/myriad/envs/PRD/data/archive/message/primo/riskerror/$cobdate";
  235.           print " Directory :$directory \n";
  236.           $riskError ="RISKERROR";
  237.           $cobdateTag = "COB_DATE";
  238.           $fileTypeTag = "FILE_TYPE";
  239.           $bookIdTag = "BOOK_ID";
  240.           $subBookIdTag ="SUB_BOOK_ID";
  241.           $ticketNumber = "TICKET_NUMBER";
  242.           $errorDescriptionTag = "ERROR_DESCRIPTION";
  243.           $errorTypeTag = "ERROR_TYPE";
  244.           $sourceSysIdTag="SOURCE_SYSTEM_ID";
  245.           $reg ="PRIMO";
  246.          }
  247.    elsif ($sourceName eq "PrimoTrade") {
  248.           $directory = "/cs/myrow/myriad/envs/PRD/data/archive/message/primo/tradeerror/$cobdate";
  249.           $tradeError="TRADEERROR";
  250.           $cobdateTag = "COB_DATE";
  251.           $fileTypeTag = "FILE_TYPE";
  252.           $bookIdTag = "BOOK_ID";
  253.           $subBookIdTag ="SUB_BOOK_ID";
  254.           $ticketNumber = "TICKET_NUMBER";
  255.           $errorDescriptionTag = "ERROR_DESCRIPTION";
  256.           $errorTypeTag = "ERROR_TYPE";
  257.           $sourceSysIdTag="SOURCE_SYSTEM_ID";
  258.           $reg ="PRIMO";
  259.          }
  260.  
  261.    elsif ($sourceName eq "Jane") {
  262.          #$directory = "/cs/myrow/myriad/envs/PRD/data/file_versions/$cobdate";
  263.          $directory = "/cs/myrch/myriad/envs/IT3/data/file_versions/$cobdate";
  264.           $riskError ="risk";
  265.           $tradeError="trade";
  266.           $surfaceError ="surface";
  267.          }
  268.    elsif ($sourceName eq "Giraffe") {
  269.           $directory = "/cs/myrow/myriad/envs/PRD/data/file_versions/$cobdate";
  270.           $riskError ="RISK";
  271.           $tradeError="TRADE";
  272.           $reg = "GIRAFFE";
  273.          }
  274.  
  275.  
  276. my $logDirectory  = "/cs/myrch/" . "/logs";
  277. # Make sure log dir exists
  278.  
  279. checkAndCreateDirPath( $logDirectory );
  280.  
  281. my $TMP_FILE="/var/tmp/checkFilePresent_$cobdate.csv";
  282.  
  283. # Make sure tmp dir exists
  284.  
  285. CreateBackupFile($TMP_FILE);
  286.  
  287.  
  288. # It generates YYYYMMDDhhmmss
  289.  
  290. my $t = localtime;
  291.  
  292.  $timeStamp = sprintf("%04d%02d%02d_%02d-%02d-%02d",
  293.                          $t->year+1900, $t->mon + 1, $t->mday,
  294.                          $t->hour, $t->min,   $t->sec );
  295.  
  296.  
  297. my $errorFileName=$sourceName.'Errors'.$timeStamp.'.html';
  298.  
  299. $zipfilename=$sourceName.'Errors'.$timeStamp.'.zip';
  300.  
  301.  
  302. my $errorFile = $logDirectory.'/'.$errorFileName;
  303.  
  304. my $tlogname = $sourceName.'Errors'.$timeStamp;
  305.  
  306. my( $runLog ) = $logDirectory.'/'.$tlogname.'.log';
  307.  
  308. open($runLogFhnd, "+>>$runLog") || logLine(LFATAL, "Failed to open '$runLog' for writing.");
  309.  
  310.  
  311. ########################################################################
  312. # Creating file for display error Details.
  313. #######u##################################################################
  314.  
  315. open(DATA,"+>>$errorFile") || die "Couldn't open file file.txt, $!";
  316.  
  317.  
  318. print DATA "<html> <TABLE BORDER> \n ";
  319.  
  320. print  DATA "<tr><td>COB Date</td><td>FO RMS</td><td>Feed Name</td><td>Book</td><td>Sub Book</td><td>GBM BOOK REF</td><td>BOOK ID</td><td>Ticket Number</td><td>Error File Type</td><td>Error Type</td><td>Error  Description</td></tr>\n";
  321.  
  322. `chmod 777 $errorFile`;
  323.  
  324.    opendir (DIR, $directory) or exit 0;
  325.  
  326. my $num= `cd $directory; ls *ERROR*xml*gz -o *error*xml.gz |wc -l`;
  327.  
  328.  
  329.        if($num >=1){
  330.        `cd $directory; gunzip *error*gz; gunzip *ERROR*gz`;
  331. }
  332.    foreach (sort grep(/^.*\.xml$/,readdir(DIR))){
  333.  
  334.         my $file = $_;
  335.  
  336. my $fileref= `grep $file* $TMP_FILE | wc -l`;
  337.  
  338. if($fileref==0)    
  339.  
  340. {
  341. if ( $file =~ m/error/i )
  342. {
  343. logLine(LINFO, "Source Name : $sourceName ");
  344.  
  345. logLine(LINFO, "Get File Name: $file ");
  346.  
  347. my @words = split /_/, $file;
  348.  
  349.  
  350. my $errorListTag;
  351.  
  352. my $errorTag;
  353.  
  354. logLine(LINFO, "Get File first word: $words[0] ,$reg ");
  355.  
  356. if ( $sourceName eq "Jane" && ( ( grep {$_ eq $tradeError} @words ) || (grep {$_ eq $riskError} @words )|| ( grep {$_ eq $surfaceError} @words )))
  357. $reg = $words[0];
  358.  
  359. logLine(LINFO, "This check is for only Jane Feed : $reg ");
  360. }
  361.  
  362. if ($words[0] =~ m/$reg/)
  363. {
  364.  
  365. print " regex is working $reg \n";
  366.  
  367.  
  368. if (grep {$_ eq $tradeError} @words) {
  369.  
  370. $errorListTag="tradeErrorList";
  371.  
  372. $errorTag="tradeError";
  373.  
  374. logLine(LINFO, "This is Trade Error: $errorListTag ");
  375.  
  376. }
  377.  
  378. else
  379.  {
  380.  
  381. $errorListTag="rmErrorList";
  382.  
  383. $errorTag="rmError";
  384.  
  385. logLine(LINFO, "This is Risk/Surface  Error: $errorListTag ");
  386. }
  387.  
  388.  
  389. my $path;
  390.  
  391. if ($words[0] eq "GIRAFFE")
  392. {
  393.  
  394.  
  395. $cobdateTag = "COB_DATE";
  396.  
  397. $fileTypeTag = "FILE_TYPE";
  398.  
  399. $bookIdTag = "BOOK_ID";
  400.  
  401. $subBookIdTag ="SUB_BOOK";
  402.  
  403. $ticketNumber = "TICKET_NUMBER";
  404.  
  405. $errorDescriptionTag = "ERROR_DESCRIPTION";
  406.  
  407. $errorTypeTag = "ERROR_TYPE";
  408.  
  409. $sourceSysIdTag ="SOURCE_SYSTEM_ID";
  410.  
  411. print "This is GIRAFFE feed \n";
  412.  
  413.  $path = "$directory/$file";
  414.  
  415. }
  416.  
  417. elsif ($words[0] ne "PRIMO")
  418. {
  419.  
  420. $cobdateTag = "cobDate";
  421.  
  422. $fileTypeTag = "fileType";
  423.  
  424. $bookIdTag = "book";
  425.  
  426. $subBookIdTag ="subBook";
  427.  
  428. $ticketNumber = "ticketNumber";
  429.  
  430. $errorDescriptionTag = "description";
  431.  
  432. $errorTypeTag = "errorType";
  433.  
  434. $sourceSysIdTag = "sourceSystemCode";
  435.  
  436. $gbmBookRefTag = "GBM_BOOK_REF";
  437.  
  438. $bookTag = "bookId";
  439.  
  440. logLine(LINFO, "This is for Jane: $sourceSysIdTag ");
  441.  
  442.  $path = "$directory/$file";
  443. }
  444. else
  445. $path = "$directory/$file";
  446.  
  447. }
  448.  
  449. logLine(LINFO, "get File complete path: $path ");
  450.  
  451. my $ererrorReport = XMLin($path,ForceArray=>['rmError','book','tradeError']);
  452.  
  453. ##########################################################################
  454. # Reading Header Values :Cobdate and File type.
  455. ##########################################################################
  456.  
  457. my $rmErrorHeaderdata=$ererrorReport->{rmErrorHeader};
  458.  
  459. #print Dumper($rmErrorHeaderdata);
  460.  
  461. my $cobdate=$rmErrorHeaderdata->{$cobdateTag};
  462.  
  463. my $errorFileType=$rmErrorHeaderdata->{$fileTypeTag};
  464.  
  465. my $forms=$rmErrorHeaderdata->{$sourceSysIdTag};
  466.  
  467. logLine(LINFO, "get Error file details CobDate ErrorFileType Fo RMS: $cobdate $errorFileType $forms ");
  468.  
  469.  
  470. ###########################################################################
  471. # Reading Body values:Book,Subbook,Ticketnumber,ErrorType,ErrorDescription
  472. ###########################################################################
  473.  
  474. my $rmErrorBodydata = $ererrorReport->{rmErrorBody};
  475.  
  476. my $bookListdata = $rmErrorBodydata->{bookList};
  477.  
  478. my $books = $bookListdata->{book};
  479.  
  480. #print Dumper($books);
  481.  
  482. foreach my $book (@{$bookListdata->{book}})
  483. {
  484. my $bookId = $book->{$bookIdTag};
  485.  
  486. my $subBookId=$book->{$subBookIdTag};
  487.  
  488. my $gbmBookRef=$book->{$gbmBookRefTag};
  489.  
  490. my $bookRef=$book->{$bookTag};
  491.  
  492. my $list = $book->{$errorListTag};
  493.  
  494. my $rmErrorData = $list->{$errorTag};
  495.  
  496. #print Dumper($rmErrorData);
  497.  
  498. foreach my $rmErrorData (@{$list->{$errorTag}})
  499.  
  500. {
  501.  
  502. my $ticketNumber = $rmErrorData->{$ticketNumber};
  503.  
  504. my $errorDesc = $rmErrorData->{$errorDescriptionTag};
  505.  
  506. my $errorType = $rmErrorData->{$errorTypeTag};
  507.  
  508. logLine(LINFO, "get Error file details  Book: $bookId  Subbook : $subBookId  TicketNumber: $ticketNumber  ErrorType : $errorType ");
  509.  
  510. print DATA "<tr><td>$cobdate</td><td>$forms</td><td>$file</td><td>$bookId</td><td>$subBookId</td><td>$gbmBookRef</td><td>$bookRef</td><td>$ticketNumber</td><td>$errorFileType</td><td>$errorType</td><td>$errorDesc</td></tr>\n";
  511.  
  512. }
  513. }
  514.  
  515. #my $bakFileName=$file.".bak";
  516.  
  517. #my $bakFile="$directory/$bakFileName";
  518.  
  519. #`mv $path $bakFile`;
  520. #open(DATA,"+>>$TMP_FILE") || die "Couldn't open file, $!";
  521.  
  522.  
  523. #print DATA "$file \n ";
  524.  
  525. #close(DATA);
  526.  
  527. }
  528. }
  529. open(DATA1,"+>>$TMP_FILE") || die "Couldn't open file, $!";
  530.  
  531.  
  532. print DATA1 "$file \n ";
  533.  
  534. close(DATA1);
  535.  
  536. }
  537. closedir(DIR);
  538. `cd $directory; gzip $file`;
  539. }
  540.  
  541. print DATA "</table></html>\n";
  542.  
  543. close (DATA);
  544.  
  545. `cd $logDirectory;zip $zipfilename  $errorFileName;chmod 777 $zipfilename`;
  546.  
  547. my $msg ='Email with ErrorDetails Attachment from Myr';
  548.  
  549. my $addr ='d.y@gmail.com';
  550.  
  551. `echo "" | mailx -s '$msg' -a '$logDirectory/$zipfilename' '$addr'`;
  552.  
  553.  
  554. logLine(LINFO, "Successfully sent mail to the user ");
Sep 14 '17 #1
Share this Question
Share on Google+
4 Replies


Expert Mod 100+
P: 588
You can't post 500+ lines of code and expect someone to troubleshoot it and fix it for you.

What part of the script is failing and in what way is it failing? What errors and/or warnings is it giving you?

Try to write a SHORT but complete script which demonstrates the problem.
Sep 14 '17 #2

Expert Mod 100+
P: 588
Get rid of the prototypes. Perl's prototypes don't work the same way as some other languages and are almost always not what you should be doing.

Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen
Sep 14 '17 #3

Expert Mod 100+
P: 588
Drop your logging routines and instead use the Log::Log4perl module.

Your getCurrentDateTimeStr() sub is not doing what you think. You should use the strftime() function from the POSIX module, which you are already loading.

Don't use TitleCase for multi word vars and subs. Use an _ underscore to separate the words and use lowercase.

Your indentation is inconsistent which makes it difficult to follow the code blocks. You can use Perl::Tidy to clean/fix the formatting.

If I look closer, I'm sure I'll find other issues that need to be addressed.
Sep 14 '17 #4

P: 2
Hi..Thanks RonB for giving your reply..As of now perl script will generate html file in IE, it is not opening in IE it is taking so much time, converting this to excel is not happening as the existing installed Perl version doesn’t support this, could you please he me in this implementing it if any.
Sep 15 '17 #5

Post your reply

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