Read a file and put it in HTML format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read a file and put it in HTML format
# 1  
Old 06-11-2010
Read a file and put it in HTML format

Hi,

I have one file as follows and I need to read this file contents in an HTML format. And send html file to some mail ids using sendmail.

Code:
                            Communications Pvt Ltd
                                     Report

AccountId    Name     Code    IdBill    Balance    Amount
-----------  -----     ------   ----    -------    -------
123123        aaaa      11       12       1000.00    1111.00
--               --         --        --         --          --

TOTAL   :                                                     1111

Can anybody help me out?
# 2  
Old 06-11-2010
What do you mean by html format?

you could simply add a pre tag around it... or do want to build a full table structure ?

You have provided your input - is this exactly the same for all inputs, or will you have multiple rows etc ?
What is your expected/preferred output?

And....What have you tried so far? Post your attempts and you will get feedback and assistance...
# 3  
Old 06-11-2010
I'd go from the opposite direction. I'd start with an html template that has the (outer elements of the) table pre-written.

Then, I'd use perl or PHP to scrape the text file and drop the listed values into rows. That works out to a file read and a loop, and all the hard work is done before you run the script.
# 4  
Old 06-11-2010
You might find following Perl script useful... Feel free to tweak it for your needs.
Code:
#!/usr/local/bin/perl

use strict;
use warnings;

my $acc=0;
my $tot=0;
my @dsplit;

my $infile='input.dat';
my $outfile='input.html';

open(I,$infile) or die "Error opening input file: $!\n";
my @data=<I>;
close(I);

open(O,">$outfile") or die "Error opening output file: $!\n";

print O "<table>\n";

foreach my $line (@data) {
chomp($line);

  if ($line =~ /^Acc/) {
  $acc=1;
  }

  if ($line =~ /^TOT/) {
  $tot=1;
  }

  if ($acc == 0) {
  print O "<tr><center>$line</center></tr>\n";
  }
  elsif ($acc == 1 && $tot == 0) {
  $line =~ s/  */ /g;
  @dsplit=split(/ /,$line);
  print O "<tr>";

  foreach my $record (@dsplit) {
  chomp($record);
  print O "<td>$record</td>";
  }
  print O "</tr>\n";
  }
  else {
  @dsplit=split(/ /,$line);
  print O "<tr><td>TOTAL:</td><td></td><td></td><td></td><td></td><td>$dsplit[-1]</td></tr>\n";
 }
}

print O "</table>";

close(O);

Code:
$ cat input.dat
                            Communications Pvt Ltd
                                     Report

AccountId    Name     Code    IdBill    Balance    Amount
-----------  -----     ------   ----    -------    -------
123123        aaaa      11       12       1000.00    1111.00
232423        abbb      21       12       1000.00    9999.00
156763        acdf      11       12       1000.00    1111.00
145633        aasw      31       12       3333.00    7777.00
235453        wwww      11       12       1000.00    1111.00
232343        asss      31       12       1000.00    1111.00
332423        eeEa      11       12       1222.00    7777.00
253123        aaaa      12       12       1000.00    1111.00
253123        adfd      11       12       1000.00    4444.00
125443        aAda      10       12       2434.00    1111.00
662123        hgja      11       12       1000.00    1000.00

TOTAL   :                                                     xxxxxx
$ 
$ ./putintable.pl
$ 
$ cat input.html
<table>
<tr><center>                            Communications Pvt Ltd</center></tr>
<tr><center>                                     Report</center></tr>
<tr><center></center></tr>
<tr><td>AccountId</td><td>Name</td><td>Code</td><td>IdBill</td><td>Balance</td><td>Amount</td></tr>
<tr><td>-----------</td><td>-----</td><td>------</td><td>----</td><td>-------</td><td>-------</td></tr>
<tr><td>123123</td><td>aaaa</td><td>11</td><td>12</td><td>1000.00</td><td>1111.00</td></tr>
<tr><td>232423</td><td>abbb</td><td>21</td><td>12</td><td>1000.00</td><td>9999.00</td></tr>
<tr><td>156763</td><td>acdf</td><td>11</td><td>12</td><td>1000.00</td><td>1111.00</td></tr>
<tr><td>145633</td><td>aasw</td><td>31</td><td>12</td><td>3333.00</td><td>7777.00</td></tr>
<tr><td>235453</td><td>wwww</td><td>11</td><td>12</td><td>1000.00</td><td>1111.00</td></tr>
<tr><td>232343</td><td>asss</td><td>31</td><td>12</td><td>1000.00</td><td>1111.00</td></tr>
<tr><td>332423</td><td>eeEa</td><td>11</td><td>12</td><td>1222.00</td><td>7777.00</td></tr>
<tr><td>253123</td><td>aaaa</td><td>12</td><td>12</td><td>1000.00</td><td>1111.00</td></tr>
<tr><td>253123</td><td>adfd</td><td>11</td><td>12</td><td>1000.00</td><td>4444.00</td></tr>
<tr><td>125443</td><td>aAda</td><td>10</td><td>12</td><td>2434.00</td><td>1111.00</td></tr>
<tr><td>662123</td><td>hgja</td><td>11</td><td>12</td><td>1000.00</td><td>1000.00</td></tr>
<tr></tr>
<tr><td>TOTAL:</td><td></td><td></td><td></td><td></td><td>xxxxxx</td></tr>
</table>$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Format text file to html

Hi Experts, Anybody out there figure out on how to achieve in shell scripts or tools. I have done googling to find solutions but no luck. I have thousands of .txt files to batch process, please see the below sample text content after -------- start here --------. What I want to achieve is to... (10 Replies)
Discussion started by: lxdorney
10 Replies

2. Shell Programming and Scripting

Convert text file to HTML tabular format.

Please provide script/commands to convert text file to HTML tabular format. No need of styles and colours, just output and a heading in table is required. Output file will be send via email and will be seen from outlook. (script required without using awk). output file content: (sar... (7 Replies)
Discussion started by: Veera_V
7 Replies

3. Shell Programming and Scripting

Read file and get the data then put it in array

Hi, I have a file called "readfile" it contains below parameters #cat readfile word=/abc=225,/abc/cba=150 three=12 four=45 five=/xyz/yza likewise multiple line. From the above file, I have to read "word" output should be like, /abc /abc/cba these values need to be put in... (3 Replies)
Discussion started by: munna_dude
3 Replies

4. Shell Programming and Scripting

Read value of a file, remove first 2 chars and put this value in a variable

Hi i have need of read a file value with cat command and remove first 2character for example cat /sys/class/rtc/day 0x12 Remove char 12 And put this value in a variable is possible with a script thanks for help (6 Replies)
Discussion started by: enaud
6 Replies

5. Shell Programming and Scripting

Displaying file in html loses format

I have a bash script to output the contents of a text file to html. Everything outputs ok, except this: ###################################################################### # # # PRIVATE/PROPRIETARY # # # # ANY UNAUTHORIZED ACCESS TO, OR MISUSE OF ABC COMPANY # # SYSTEMS OR DATA MAY... (2 Replies)
Discussion started by: numele
2 Replies

6. Shell Programming and Scripting

how to display the output file in an html format using perl

Hi, I have written a perl script to dispaly some statements from a file but i want the output statements to be dispalyed in an HTML format.Is it possible for me to do in perl scripting? Please help me with ur thoughts. Thanks In Advance Meva. (1 Reply)
Discussion started by: meva
1 Replies

7. Shell Programming and Scripting

Format txt file as html table

I have a short time to solve a problem, so I need some help. I've searched the forum, but I couldn't find a solution to my problem. I made a script to filter some text and now I have a new requirement to make it available as html table. Problem is that I more than one files with different set... (2 Replies)
Discussion started by: tetreb
2 Replies

8. Shell Programming and Scripting

Read multiple log files and create output file and put the result

OS : Linux 2.6.9-67 - Red Hat Enterprise Linux ES release 4 Looking for a script that reads the following log files that gets generated everynight between 2 - 5am Master_App_20090717.log Master_App1_20090717.log Master_App2_20090717.log Master_App3_20090717.log... (2 Replies)
Discussion started by: aavam
2 Replies

9. Shell Programming and Scripting

read a file in shell and put result in a line

Hi All, I have a to read a file and put the result in one line. The i am reading from contain the data like below. 1 signifies the beging of the new line. (6 Replies)
Discussion started by: lottiem
6 Replies

10. UNIX for Dummies Questions & Answers

I want some selected data from first file and put into other file in specified format

I have a file with follwing content ---------------------------------- SCHEDULE XXXXXXXXX#JOBCOUNT ON EVERYDAY AT 0000 PRIORITY 50 SCHEDULE XXXXXXXXX#ABCDEFGH ON EVERYDAY AT 0001 PRIORITY 29 SCHEDULE... (5 Replies)
Discussion started by: shreyas
5 Replies
Login or Register to Ask a Question