Splitting csv into 3 tables in html file

I have the data in csv in 3 tables. how can I output the same into 3 tables in html.also how can I set the width. tried multiple options . attached is the format.

 #!/bin/ksh
 awk 'BEGIN{
        FS=","
        print "<HTML><BODY><TABLE border = '1' cellpadding=10 width=100>"
   print "<head><style> table {border-collapse: collapse;} table, td, th {border: 1px solid black;} </style></head>"
     }
     {
        printf "<TR>"
        for(i=1;i<=NF;i++)
          printf "<TD>%s</TD>", $i
        print "</TR>"
     }
     END{
        print "</TABLE></BODY></HTML>"
     }
     ' report.csv > file2.html

Your awk needs to be changed slightly and put inside a loop:
Here is a simple sample. It writes everything to single html file, from 3 input files

#!/bin/ksh
echo "<HTML><BODY>" >> file1.html
for f in report.csv report2.csv  report3.csv   #loop
do

 awk 'BEGIN{
        FS=","
        print "<TABLE border = '1' cellpadding=10 width=100>"
   print "<head><style> table {border-collapse: collapse;} table, td, th {border: 1px solid black;} </style></head>"
     }
     {
        print "<TR>"
        for(i=1;i<=NF;i++)
        {
          printf ("<TD>%s</TD>\n", $i)
        }
        print "</TR>"
     }
     END{ print "</TABLE>" }
     ' "$f"  >> file1.html
done
echo "</BODY></HTML>"  >> file1.html
     

Please define 'changing width'.

Is it possible to generate from 1 csv instead of 3 csv's?

If you have 3 tables in 1 CSV file, how are the tables separated?

Maybe it would help if you showed us a sample input file and the output you hope to produce from that input.

how can i change the width of the columns in the above.

---------- Post updated at 12:18 PM ---------- Previous update was at 12:17 PM ----------

tables in the csv are separated by space. if that is tough i can output the tables in 3 separate csv's and then create a single html from 3 csv files.

I am still not able to define width of the columns in the above.

---------- Post updated at 12:27 PM ---------- Previous update was at 12:18 PM ----------

print "<head><style> table {border-collapse: collapse; width:150%;} table, td, th {border: 1px solid black;} </style></head>"

when i add the above i changes width for the 1st and 3rd table. not the 2nd table. also how can i change the width for each table separately

 <table>
  <col width="130">
  <col width="80">
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
  <tr>
    <td>February</td>
    <td>$80</td>
  </tr>
</table> 

Or:

  <colgroup>
    <col span="2" style="background-color:white">
    <col style="background-color:yellow">
  </colgroup>

You can do this. But it requires that you know the max number of columns possible because you specify col value for each column. Empty columns are still that wide, too. colgroup will set the width value for the whole table.

the above is not working. also i have around 60 columns. cant define for each

So you can't define the size for each field you want (with no explanation of why not), and the code you have isn't working, and you won't give us representative sample input and corresponding desired output. Under these conditions, how do you expect us to help you write software that will produce the output you want?

Please:

  1. explain how you would decide by looking at an input file what width=value should be assigned for a given column of data,
  2. provide a small, representative sample input CSV file (in CODE tags),
  3. provide a corresponding sample output HTML file (in CODE tags) that you want to produce from that sample input CSV file, and
  4. more clearly define how tables in your input CSV files are separated (Do you mean that any line containing a <space> character separates tables? Or, that a line containing only characters in the current locale's space character class separates tables? Or, that an empty line separates tables?)
1 Like