Monday, January 28, 2008

Converting to HTML table code


I had a file like this

$ cat testf.txt
First field
Second field
Third field
4th field
fifth field

I was interested to make a HTML table of the contents of the above file "testf.txt"

1 First field

2 Second field

3 Third field

4 4th field

5 fifth field


This is how I converted the above "testf.txt" into a HTML code to generate a table like above.

$ awk 'BEGIN {print "<table border="1">"} END {print "</table>"}{print "<tr>\n<td>"NR,$0"</td>\n</tr>"}' testf.txt > testf.html

The contents of testf.html will be like this

<table border=1>
<tr>
<td>1 First field</td>
</tr>
<tr>
<td>2 Second field</td>
</tr>
<tr>
<td>3 Third field</td>
</tr>
<tr>
<td>4 4th field</td>
</tr>
<tr>
<td>5 fifth field</td>
</tr>
</table>



2 comments:

tiru said...

this command not working

Jadu Saikia said...

@tiru, could you please paste here the error you are getting. Thanks.

© Jadu Saikia www.UNIXCL.com