getdpg
December 19, 2006, 7:01am
1
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
anbu23
December 19, 2006, 7:30am
2
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
ripat
December 19, 2006, 7:30am
3
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.
getdpg
December 19, 2006, 8:11am
5
pls can u tell me how to run this code
regards
dips
getdpg
December 19, 2006, 8:19am
6
anbu23 please can you tell me how to run this script, it is giving me error on 1 line.
aju_kup
December 19, 2006, 8:23am
8
while use -v option with awk it is also giving err to me also
try to use nawk in place of awk
getdpg
December 19, 2006, 8:26am
9
awk: syntax error near line 1
awk: bailing out near line 1
anbu23
December 19, 2006, 8:30am
10
Replace awk by nawk in the above script
getdpg
December 19, 2006, 12:23pm
11
it is not working .Please give me soultions ,it is not homework. I have to craete webpage for different logs in my company eroom.
anbu23
December 20, 2006, 1:09am
12
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>"