Solved

Convert records into html table using Perl

Posted on 2011-02-16
4
291 Views
Last Modified: 2012-05-11
I have the following data record in a scsi_test file that I would like to convert into a html table using perl

Here's the data record....

__Data__
scsi_test_hotswap
stop
Passed
1. dldldld - dldldldldldldldld
2. dldldld - dldldldldlldldldld
1. dldldld - dldldldldldldldld
2. dldldld - dldldldldlldldldld

__Data__
scsi_test_memory_leakage
stop
Passed
1. dldldld - dldldldldldldldld
2. dldldld - dldldldldlldldldld
3. dldldld - dldldldldldldldld
4. dldldld - dldldldldlldldldld

__Data__
scsi_test_scatter_gatter


Starting Code...
--------------------
#!/usr/bin/perl

use warnings;
use strict;

# Html table tag variables

my $open_table ="<table border=\"1\">";
my $close_table ="</table>";
my $open_row ="<tr>";
my $close_row ="</tr>";
my $open_column_header ="<th>";
my $close_column_header ="</th>";
my $open_column_data ="<td>";
my $close_column_data ="</td>";
my $open_color_blue ="<font color=\"#0000ff\">";
my $close_color_blue ="</font>";
my $blank_column ="<tr></tr>";


my @first_record_row; # this should include the first element and element starting with 1. from the whole_record array
                        excluding the __Data__ input record separator.
my @additional_record_rows; # this should include the remaining elements in the whole_record array.

# Input Record Separator
$/='__Data__';


# Read In file

open FH,'<',"scsi_test" or die $!;


while(<FH>)
{
        s/^\s+//;
        s/^\s+$//;

   
    print "@whole_record","\n";

    foreach (@whole_record)
    {
       print $whole_record[1],"\n";
      # push @first_record_row = grep(m/^1\..*/,@whole_record);
      # print @first_record_row;

    }


Output should look like this....

row_1 -----> scsi_test_hotswap                  Passed          1. dldldld - dldldldldldldldld
row_2 ----->                                                                      2. dldldld - dldldldldldldldld
row_3 ----->                                                                      3. dldldld - dldldldldldldldld
row_4 ----->                                                                      4. dldldld - dldldldldldldldld
row_5 -----> blank line row
row_6 -----> scsi_test_memory_leakage     Passed          1. dldldld - dldldldldldldldld          
row_7 ----->                                                                       2. dldldld - dldldldldldldldld
row_8 ----->                                                                       3. dldldld - dldldldldldldldld

Side note, the text upto the hypen should be color blue blod as in, dldldld -, and excluding the line number.
0
Comment
Question by:areyouready344
  • 2
  • 2
4 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 34911346
This should give you a start.

To make it easier to test I changed it so it takes input from stdin, so run it like this:

    perl makeTab.pl scsi.test >scsiTest.html

#!/usr/bin/perl


# Html table tag variables

my $open_table ="<table border=\"1\">";
my $close_table ="</table>";
my $open_row ="<tr>";
my $close_row ="</tr>";
my $open_column_header ="<th>";
my $close_column_header ="</th>";
my $open_column_data ="<td>";
my $close_column_data ="</td>";
my $open_color_blue ="<font color=\"#0000ff\">";
my $close_color_blue ="</font>";
my $blank_column ="<tr></tr>";


print $open_table;

while(<>)
{
	s/[\r\n]//g;
	if ( /__Data__/ )
	{
		$testname = <>;
		if ( $testname eq '' ) { last; }
		$testname =~ s/[\r\n]//g;
		
		$stopline = <>;
		if ( $stopline eq '' ) { last; }
		$stopline =~ s/[\r\n]//g;
		if ( $stopline ne 'stop' ) { die "expected stop\n"; }

		$pass = <>;
		if ( $pass eq '' ) { last; }
		$pass =~ s/[\r\n]//g;

		while ( <> )
		{
			if ( ! ( /^[0-9]/ ) ) { last; }

			$detailline = $_;

			print $open_row;
			print $open_column_data;
			print $testname;
			print $close_column_data . $open_column_data;
			print $pass;
			print $close_column_data . $open_column_data;
			print $detailline;
			print $close_column_data . $close_row;


		}
	}
}

print $close_table;

Open in new window

0
 

Author Comment

by:areyouready344
ID: 34912095
thanks but did not work, got the output of

<table border="1"><table border="1">

I'll try it again later
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 34912174
A little cleaner.

#!/usr/bin/perl


# Html table tag variables

my $open_table ="<table border=\"1\">";
my $close_table ="</table>";
my $open_row ="<tr>";
my $close_row ="</tr>";
my $open_column_header ="<th>";
my $close_column_header ="</th>";
my $open_column_data ="<td>";
my $close_column_data ="</td>";
my $open_color_blue ="<font color=\"#0000ff\">";
my $close_color_blue ="</font>";
my $blank_column ="<tr></tr>";


print $open_table;

while(<>)
{
	s/[\r\n]//g;
	if ( /__Data__/ )
	{
		$testname = <>;
		if ( $testname eq '' ) { last; }
		$testname =~ s/[\r\n]//g;
		
		$stopline = <>;
		if ( $stopline eq '' ) { last; }
		$stopline =~ s/[\r\n]//g;
		if ( $stopline ne 'stop' ) { die "**** expected stop line not founc\n"; }

		$pass = <>;
		if ( $pass eq '' ) { last; }
		$pass =~ s/[\r\n]//g;

		while ( <> )
		{
			if ( ! ( /^[0-9]/ ) ) { $quit = 1; last; }

			$detailline = $_;
			$detailline =~ s/[\r\n]//g;

			print $open_row;
			print $open_column_data;
			print $testname;
			print $close_column_data . $open_column_data;
			print $pass;
			print $close_column_data . $open_column_data;
			print $detailline;
			print $close_column_data . $close_row;
			print "\n";
		}
		if ( $quit && ( $_ eq '' ) ) { last; }
	}
}

print $close_table;

Open in new window


The output:

c:\temp>perl makeTab.pl scsi_test
<table border="1"><tr><td>scsi_test_hotswap</td><td>Passed</td><td>1. dldldld - dldldldldldldldld</td></tr>
<tr><td>scsi_test_hotswap</td><td>Passed</td><td>2. dldldld - dldldldldlldldldld</td></tr>
<tr><td>scsi_test_hotswap</td><td>Passed</td><td>1. dldldld - dldldldldldldldld</td></tr>
<tr><td>scsi_test_hotswap</td><td>Passed</td><td>2. dldldld - dldldldldlldldldld</td></tr>
<tr><td>scsi_test_memory_leakage</td><td>Passed</td><td>1. dldldld - dldldldldldldldld</td></tr>
<tr><td>scsi_test_memory_leakage</td><td>Passed</td><td>2. dldldld - dldldldldlldldldld</td></tr>
<tr><td>scsi_test_memory_leakage</td><td>Passed</td><td>3. dldldld - dldldldldldldldld</td></tr>
<tr><td>scsi_test_memory_leakage</td><td>Passed</td><td>4. dldldld - dldldldldlldldldld</td></tr>
</table>
c:\temp>

Open in new window

0
 

Author Closing Comment

by:areyouready344
ID: 35036824
excellent answer
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now