Creating html table from data in file

Hi. I need to create html table from file which contains data. No awk please :slight_smile: In example,

->cat file
num1 num2 num3
23 3 5 
2 3 4 

(between numbers and words single TAB).
after running mycode i need to get (heading is the first line):
<table> <tr><th>num1</th><th>num2</th><th>num3</th></tr>
<tr><td>23</td><td>3</td><td>5</td></tr>
<tr><td>2</td><td>3</td><td>4</td></tr>
</table>

My code looks like this....but I stopped here...

for i in $*
do
echo "<table>"
while read line
do

for word in $line
do
echo "<tr>"
echo $word
echo "</tr>"
#echo $line
echo "</table>"

done
done<$i

done

Hi.

When you have restrictions it is useful to say why you must or must not use certain approaches.

See the links at the bottom of this thread, as well as Need to convert output.txt into html file for some ideas ... cheers, drl

Try this:

for i in $*
do
    echo "<table>"
    ln=1
    OIFS=$IFS
    IFS=$'\t'
    while read line
    do
        echo "<tr>"
        for word in $line
        do
            if [ $ln -eq 1 ]
            then
                echo "<th>$word</th>"
            else
                echo "<td>$word</td>"
            fi
        done
        echo "</tr>"
        let ln=ln+1
    done<$i
    IFS=$OIFS
    echo "</table>"
done