Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Taking information from a text file and inserting it into an html template

by wruehl (Acolyte)
on Dec 13, 2007 at 14:38 UTC ( [id://656828]=perlquestion: print w/replies, xml ) Need Help??

wruehl has asked for the wisdom of the Perl Monks concerning the following question:

I've been tasked with taking some output data from a script and putting it into an html report e-mail for our manager. The first step is to be able to parse from the input file, which will have a format as below: I can do things like remove the date and the lines that say Fault report for x . I'm thinking there must be a way to take in a line to memory and then insert it into an html template in a specific location. I'm just not sure how to do it. The once thing I can't change is the extra line that gets printed after The array is operating normally, that is a function of the reporting tool.

Thu Dec 13 07:03:01 EST 2007 Fault Reports for RDC: Fault Report for SA220: The array is operating normally. Fault Report for SA221: The array is operating normally. Fault Report for SA222: The array is operating normally. Fault Reports for TDC: Fault Report for SA120: The array is operating normally. Fault Report for SA121: The array is operating normally. Fault Report for SA122: The array is operating normally.
I have an HTML template file ready to go. It can be found here:

https://netfiles.uiuc.edu/wruehl/www/EMC.html.tt

I want to be able to pipe in the data from the lines in the text file to the corresponding fields in the html template.

Update: I have some code that is used in a similar manner. If F_INFILE is the input from above, how could I do the search and input like it is done here? I'm not very good with regex stuff, so this looks foreign to me: What is being done here? I know it is going through line by line in the source file, but beyond that I'm not sure, is it searching for a line that contains raidExMibVersion and then piping it to the parameter to be passed?:

open(F_INFILE_3, "< $OUTFILE_3"); open(F_INFILE_8, "< $OUTFILE_8"); $PARAMS{MTIME} = $theTime; foreach (<F_INFILE_3>) { chomp $_; $_ =~ s /^\s//g; $_ =~ s /\./_/g; if ( $_ =~ m/raidExMibVersion/g ) { @TEMP_ARRAY_1 = split("=",$_); @TEMP_ARRAY_2 = split(":",$TEMP_ARRAY_1[1]); $PARAMS{MV} = $TEMP_ARRAY_2[1]; }
-Bill

Replies are listed 'Best First'.
Re: Taking information from a text file and inserting it into an html template
by wfsp (Abbot) on Dec 13, 2007 at 15:43 UTC
    This is based on the data you posted. Your update appeared while I was writing.

    #!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; $Data::Dumper::Indent = 1; use HTML::Template; my $date = <DATA>; my $str = do{local $/;<DATA>}; my @recs = split /Fault Reports/, $str; my @type_params; for my $rec (@recs){ next unless $rec; my @flds = split /Fault Report/, $rec; my $type = shift @flds; chomp $type; my @fld_params; for my $fld (@flds){ push @fld_params, {fld => join q{ }, split(/\n/, $fld)}; } push @type_params, { type => $type, flds => \@fld_params, } } my $t = HTML::Template->new(filename => q{test.html}); $t->param( date => $date, types => \@type_params, ); print $t->output; __DATA__ Thu Dec 13 07:03:01 EST 2007 Fault Reports for RDC: Fault Report for SA220: The array is operating normally. Fault Report for SA221: The array is operating normally. Fault Report for SA222: The array is operating normally. Fault Reports for TDC: Fault Report for SA120: The array is operating normally. Fault Report for SA121: The array is operating normally. Fault Report for SA122: The array is operating normally.
    template:
    <html> <head> <title>email</title> </head> <body> <p>date: <TMPL_VAR date></p> <TMPL_LOOP types> <p><TMPL_VAR type></p> <ul> <TMPL_LOOP flds> <li><TMPL_VAR fld></li> </TMPL_LOOP> </ul> </TMPL_LOOP> </body> </html>
    output: (some whitespace removed)
    <html> <head> <title>email</title> </head> <body> <p>date: Thu Dec 13 07:03:01 EST 2007</p> <p> for RDC:</p> <ul> <li> for SA220: The array is operating normally.</li> <li> for SA221: The array is operating normally.</li> <li> for SA222: The array is operating normally.</li> </ul> <p> for TDC:</p> <ul> <li> for SA120: The array is operating normally.</li> <li> for SA121: The array is operating normally.</li> <li> for SA122: The array is operating normally.</li> </ul> </body> </html>

      He's already using Template Toolkit, having him switch to HTML::Template seems like more work than necessary for him.

      Frank Wiles <frank@revsys.com>
      www.revsys.com

Re: Taking information from a text file and inserting it into an html template
by ides (Deacon) on Dec 13, 2007 at 18:15 UTC

    Hi Bill. All you really need to do is gather up the data from your file into some data structure. If you just need to put the lines as is ( or slightly modified ) an array is probably perfect. If you need to break these lines up into more granular pieces you will probably want an array of hashes with the hashes containing the individual data elements.

    Then you can use MIME::Lite::TT::HTML to send your E-mail using a Template Toolkit template. I wrote a short tutorial on how to use this module which can be found here

    Once you have the data in some sort of structure, you just pass that data as TmplParams and then can access it in the template for position, loops, etc.

    Hope this helps!

    Frank Wiles <frank@revsys.com>
    www.revsys.com

      Thanks for the reccomendation. This is what I have so far, it is mostly cannibalized from a similar reporting scheme. The comparison tags in here are from the original script, I will not be searching for things like dkcHWProcessor in my script.

      I just need to figure out how to get the line after something is found, IE the line directly after SA220 Fault report, and place it into the array entry. I also need to be able to check that field and compare to "The array is operating normally." in order to flag for the summary.

      use strict; use Time::Local; use File::Basename; use File::Spec; use Cwd; use Getopt::Long; use constant TRUE => 1; use constant FALSE => 0; use MIME::Lite::TT::HTML; use warnings; my $OUTFILE_3 = "/EMC/faults.txt"; my $OUTFILE_5 = "EMCerrors.txt"; my $OUTFILE_6 = "EMCerrorlog.txt"; my $OUTFILE_7 = "EMCmailtxt.txt"; my $OUTFILE_8 = "/EMC/simrc.txt"; # Get the current time and date my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my @weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun); my $second = undef; my $minute = undef; my $hour = undef; my $dayOfMonth = undef; my $month = undef; my $yearOffset = undef; my $dayOfWeek = undef; my $dayOfYear = undef; my $daylightSavings = undef; ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek +, $dayOfYear, $daylightSavings) = localtime(); my $year = 1900 + $yearOffset; my $theTime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $months[$m +onth] $dayOfMonth, $year"; my @TEMP_ARRAY_1 = undef; my @TEMP_ARRAY_2 = undef; my @TEMP_ARRAY_3 = undef; my $SRCHSTR = undef; my $SRCHSTR1 = undef; my $SRCHSTR2 = undef; my $SRCHSTR3 = undef; my $PRTSTR = undef; my %PARAMS; open(F_INFILE_3, "< $OUTFILE_3"); open(F_INFILE_8, "< $OUTFILE_8"); $PARAMS{MTIME} = $theTime; foreach (<F_INFILE_3>) { chomp $_; $_ =~ s /^\s//g; $_ =~ s /\./_/g; if ( $_ =~ m/dkcHWProcessor/g ) { @TEMP_ARRAY_1 = split("=",$_); @TEMP_ARRAY_2 = split(":",$TEMP_ARRAY_1[1]); $PARAMS{SA120} = $TEMP_ARRAY_2[1]; if ( $PARAMS{SA120} =~ m/noError/g ) { } else { $PARAMS{SUMMARY} = $PARAMS{SUMMARY}= "SA120;"; } } if ( $_ =~ m/dkcHWCSW/g ) { @TEMP_ARRAY_1 = split("=",$_); @TEMP_ARRAY_2 = split(":",$TEMP_ARRAY_1[1]); $PARAMS{SA121} = $TEMP_ARRAY_2[1]; if ( $PARAMS{SA121} =~ m/noError/g ) { } else { $PARAMS{SUMMARY} = $PARAMS{SUMMARY}= "SA121;"; } } if ( $_ =~ m/dkcHWCache/g ) { @TEMP_ARRAY_1 = split("=",$_); @TEMP_ARRAY_2 = split(":",$TEMP_ARRAY_1[1]); $PARAMS{SA122} = $TEMP_ARRAY_2[1]; if ( $PARAMS{SA122} =~ m/noError/g ) { } else { $PARAMS{SUMMARY} = $PARAMS{SUMMARY}= "SA122;"; } } if ( $_ =~ m/dkcHWSM/g ) { @TEMP_ARRAY_1 = split("=",$_); @TEMP_ARRAY_2 = split(":",$TEMP_ARRAY_1[1]); $PARAMS{SA210} = $TEMP_ARRAY_2[1]; if ( $PARAMS{SA210} =~ m/noError/g ) { } else { $PARAMS{SUMMARY} = $PARAMS{SUMMARY}= "SA220;"; } } if ( $_ =~ m/dkcHWPS/g ) { @TEMP_ARRAY_1 = split("=",$_); @TEMP_ARRAY_2 = split(":",$TEMP_ARRAY_1[1]); $PARAMS{SA211} = $TEMP_ARRAY_2[1]; if ( $PARAMS{SA211} =~ m/noError/g ) { } else { $PARAMS{SUMMARY} = $PARAMS{SUMMARY}= "SA221;"; } } if ( $_ =~ m/dkcHWBattery/g ) { @TEMP_ARRAY_1 = split("=",$_); @TEMP_ARRAY_2 = split(":",$TEMP_ARRAY_1[1]); $PARAMS{SA212} = $TEMP_ARRAY_2[1]; if ( $PARAMS{SA212} =~ m/noError/g ) { } else { $PARAMS{SUMMARY} = $PARAMS{SUMMARY}= "SA222;"; } } } close F_INFILE_3; if ( $PARAMS{SUMMARY} eq "" ) { $PARAMS{SUMMARY} = "NO ERRORS FOUND"; my $msg = MIME::Lite::TT::HTML->new( From => 'santeam@bla.com', To => 'pbkhk@bla.com', Subject => 'EMC CX HEALTH REPORT:NO ERRORS FOUND', Template => { html => 'EMC.html.tt', }, TmplParams => \%PARAMS, ); $msg->send; } else { $PARAMS{SUMMARY} = "ERRORS FOUND:".$PARAMS{SUMMARY}; my $msg = MIME::Lite::TT::HTML->new( From => 'santeam@bla.com', To => 'pbkhk@bla.com', Subject => 'EMC CX HEALTH REPORT:ERRORS FOUND', Template => { html => 'EMC.html.tt', }, TmplParams => \%PARAMS, ); $msg->send; };
      -Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://656828]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-24 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found