# Splitting csv into 3 tables in html file

**URL:** <https://community.unix.com/t/splitting-csv-into-3-tables-in-html-file/367651>\
**Category:** Shell Programming and Scripting\
**Created:** [July 29, 2017, 12:56pm UTC](https://community.unix.com/t/splitting-csv-into-3-tables-in-html-file/367651 "2017-07-29T12:56:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![archana25](https://community.unix.com/letter_avatar/archana25/32/6_449c533f586939b6e5f246632a46421f.png) [@archana25](https://community.unix.com/u/archana25)\
**Post date:** [July 29, 2017, 12:56pm UTC](https://community.unix.com/t/splitting-csv-into-3-tables-in-html-file/367651/1 "2017-07-29T12:56:30Z")

</div>

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.

```nohighlight
 #!/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

```

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/6_449c533f586939b6e5f246632a46421f.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [July 29, 2017, 5:25pm UTC](https://community.unix.com/t/splitting-csv-into-3-tables-in-html-file/367651/2 "2017-07-29T17:25:19Z")

</div>

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

```nohighlight
#!/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'.

---

<div class="post-metadata">

**Author:** ![archana25](https://community.unix.com/letter_avatar/archana25/32/6_449c533f586939b6e5f246632a46421f.png) [@archana25](https://community.unix.com/u/archana25)\
**Post date:** [July 30, 2017, 8:11am UTC](https://community.unix.com/t/splitting-csv-into-3-tables-in-html-file/367651/3 "2017-07-30T08:11:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [July 30, 2017, 11:39am UTC](https://community.unix.com/t/splitting-csv-into-3-tables-in-html-file/367651/4 "2017-07-30T11:39:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![archana25](https://community.unix.com/letter_avatar/archana25/32/6_449c533f586939b6e5f246632a46421f.png) [@archana25](https://community.unix.com/u/archana25)\
**Post date:** [July 30, 2017, 12:27pm UTC](https://community.unix.com/t/splitting-csv-into-3-tables-in-html-file/367651/5 "2017-07-30T12:27:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/6_449c533f586939b6e5f246632a46421f.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [July 30, 2017, 12:44pm UTC](https://community.unix.com/t/splitting-csv-into-3-tables-in-html-file/367651/6 "2017-07-30T12:44:47Z")

</div>

```nohighlight
 <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:

```nohighlight
  <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.

---

<div class="post-metadata">

**Author:** ![archana25](https://community.unix.com/letter_avatar/archana25/32/6_449c533f586939b6e5f246632a46421f.png) [@archana25](https://community.unix.com/u/archana25)\
**Post date:** [July 30, 2017, 12:52pm UTC](https://community.unix.com/t/splitting-csv-into-3-tables-in-html-file/367651/7 "2017-07-30T12:52:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [July 30, 2017, 11:02pm UTC](https://community.unix.com/t/splitting-csv-into-3-tables-in-html-file/367651/8 "2017-07-30T23:02:08Z")

</div>

> [@archana25](#):
>
> 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?)
