Solved

Convert records into html table using Perl

Posted on 2011-02-16
4
305 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

911 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

18 Experts available now in Live!

Get 1:1 Help Now