# help with a bash script to create a html table

**URL:** <https://community.unix.com/t/help-with-a-bash-script-to-create-a-html-table/261160>\
**Category:** Shell Programming and Scripting\
**Created:** [March 6, 2010, 12:34pm UTC](https://community.unix.com/t/help-with-a-bash-script-to-create-a-html-table/261160 "2010-03-06T12:34:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dunryc](https://community.unix.com/letter_avatar/dunryc/32/5_5575768a8748004e209b776fc1b2916d.png) [@dunryc](https://community.unix.com/u/dunryc)\
**Post date:** [March 6, 2010, 12:34pm UTC](https://community.unix.com/t/help-with-a-bash-script-to-create-a-html-table/261160/1 "2010-03-06T12:34:43Z")

</div>

Hi guys as the title says i need a little help i have partisally written a bash script to create a table in html

> [@](#):
>
> echo "\<table\>" \> table.html
> 
> for ((rows=1; rows\<=$1; rows++));  
> do  
> echo "\<tr\>" \>\> table.html
> 
> ```
> for \(\(columns=1; columns&lt;=$2; columns\+\+\)\);
> do
> echo "&lt;td&gt;A&lt;/td&gt;" &gt;&gt; table.html
> 
> done
> 
> echo "&lt;/tr&gt;" &gt;&gt; table.html
> 
> ```
> 
> done
> 
> echo "\</table\>" \>\> table.html

so if i use ./test 3,3

i get the following output

> [@](#):
>
> A A A  
> A A A  
> A A A

for the third arguement in the script i wish to include content that will be replace the A characters in the table this will be in the form of a text file as below

> [@](#):
>
> Apples  
> Oranges  
> Bananas  
> Pears  
> Kiwis  
> Plums  
> Peaches  
> Grapes  
> Melons

these would appear in table for as above can any one please give me a heads up as to how i can accomplish this ? Cheers guys

---

<div class="post-metadata">

**Author:** ![dr.house](https://community.unix.com/letter_avatar/dr.house/32/5_5575768a8748004e209b776fc1b2916d.png) [@dr.house](https://community.unix.com/u/dr.house)\
**Post date:** [March 6, 2010, 2:01pm UTC](https://community.unix.com/t/help-with-a-bash-script-to-create-a-html-table/261160/2 "2010-03-06T14:01:39Z")

</div>

A point to start from (?):

```plaintext
#! /bin/bash

ROWS=3
COLS=3

LIST=(
  [11]="Apples" [12]="Oranges" [13]="Bananas"
  [21]="Pears" [22]="Kiwis" [23]="Plums"
  [31]="Peaches" [32]="Grapes" [33]="Melons"
)

echo "<table>"
for ((RI=1; RI<=$ROWS; RI++))
do
  echo -e "\t<tr>"
  for ((CI=1; CI<=$COLS; CI++))
  do
    echo -e "\t\t<td>${LIST[$RI$CI]}</td>"
  done
  echo -e "\t</tr>"
done
echo "</table>"

exit 0

```

```plaintext
[house@leonov] bash test.bash
<table>
        <tr>
                <td>Apples</td>
                <td>Oranges</td>
                <td>Bananas</td>
        </tr>
        <tr>
                <td>Pears</td>
                <td>Kiwis</td>
                <td>Plums</td>
        </tr>
        <tr>
                <td>Peaches</td>
                <td>Grapes</td>
                <td>Melons</td>
        </tr>
</table>

```

(Reference: [Arrays](http://tldp.org/LDP/abs/html/arrays.html))

---

<div class="post-metadata">

**Author:** ![cfajohnson](https://community.unix.com/user_avatar/community.unix.com/cfajohnson/32/16669_2.png) [@cfajohnson](https://community.unix.com/u/cfajohnson)\
**Post date:** [March 6, 2010, 8:35pm UTC](https://community.unix.com/t/help-with-a-bash-script-to-create-a-html-table/261160/3 "2010-03-06T20:35:37Z")

</div>

```nohighlight
{
  echo "<table>"
  printf "<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n" $(cat "$file")
  echo "</table>"
} > table.html

```
