Need help to convert Flat file to HTML

Hello

I need help to convert flat file data to HTML Table format.
I am generating everyday Flat file and want to convert into HTML Table format.

The format of my file is:

version host Total YRS NO APPS PSD
10 Sun 30 2 4 6 7

and flat file is TAB delimentaed.

Please do help me

awk -v border=5 -v width=10 -v bgcolor="black" -v fgcolor="red" '
BEGIN { printf("<table border=\"%d\" bordercolor=\"%s\" width=\"%d\" bgcolor=\"%s\n>",border,bgcolor,width,fgcolor) }
{       print "<tr>";
        for( i = 1 ; i <= NF ; ++i )
                print "<td> "$i" </td>"
        print "</tr>"
}
END {  print "</table>" } ' file

Try this:

$ awk -f flat2html.awk yourFile > output.html

flat2html.awk

BEGIN {
    FS="\t"
    print "<table>\n"
    format="<tr>\n\t<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>\n</tr>\n"
}

{
    printf (format, $1, $2, $3, $4, $5, $6, $7)
}

END {
    print "</table>"
}
<table>[manually echo]
<tr><td>[manually echo]version	[tab -> </td><td>]host	[tab -> </td><td>]Total	[tab -> </td><td>]YRS	[tab -> </td><td>]NO	[tab -> </td><td>]APPS	[tab -> </td><td>]PSD[\n -> </td></tr>]
<tr><td>[manually echo]10	[tab -> </td><td>]Sun	[tab -> </td><td>]30	[tab -> </td><td>]2	[tab -> </td><td>]4	[tab -> </td><td>]6	[tab -> </td><td>]7[\n -> </td></tr>]
</table>[manually echo]

use sed:

% sed s/WHAT/TO/g

or awk:

% awk '{gsub(/WHAT/,"TI",$0); print}'

so script will do:

manually place
<table> before parsing this file,
parse the file echo <tr><td> before each line, change TAB -> </td><td>, change \n -> </td></tr>.
after parsing the file echo </table>

done.

pls can u tell me how to run this code
regards
dips

anbu23 please can you tell me how to run this script, it is giving me error on 1 line.

Can you show the error?

while use -v option with awk it is also giving err to me also

try to use nawk in place of awk

awk: syntax error near line 1
awk: bailing out near line 1

Replace awk by nawk in the above script

it is not working .Please give me soultions ,it is not homework. I have to craete webpage for different logs in my company eroom.

Check in your system what version of awk do you have and replace awk by that version in the code

or

use this

sed "s/\t/ /g;s/^/<tr>/;s/$/<\/tr>/" file | sed "s/\([^ ]*\)/<td>\1<\/td>\\
/g" | sed "1 i\\
<table border=5 bordercolor=\"red\" width=10 bgcolor=\"black\">
$ a\\
</table>"