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>
<td>1 First field</td>
<td>2 Second field</td>
<td>3 Third field</td>
<td>4 4th field</td>
<td>5 fifth field</td>


tiru said...

this command not working

Unknown said...

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

© Jadu Saikia