jimmyf
June 17, 2014, 5:44pm
1
I have a script which converts a .csv file to html nicely. Trying to add 3 colors, green, yellow and red to the output depending upon the values in the cells. Tried some printf command but just can't seem to get any where. Any ideas would be appreciated.
nawk 'BEGIN{
FS=","
print "<HTML>""<TABLE border="1"><TH>Col1</TH><TH>Col2</TH><TH>Col3</TH><TH>Col4</TH><TH>Col5</TH><TH>Col6</TH><TH>Col7</TH><TH>Col8</TH><TH>Col9</TH><TH>Col10</TH><TH>Col11</TH>"
}
{
printf "<TR>"
for(i=1;i<=NF;i++)
printf "<TD>%s</TD>", $i
print "</TR>"
}
END{
print "</TABLE></BODY></HTML>"
}
' a.csv > 1.html
Please show the input you have and the output you want.
Yoda
June 17, 2014, 6:05pm
3
For table cell color use HTML td tag and bgcolor attribute.
For table cell font color use HTML font tag and color attribute.
How about this as an example. this changes all cells and values >80 are red; > 60 are yellow and >30 are green:
nawk 'BEGIN {
FS=","
printf "<HTML>\n<body>\n<table border=\"1\" cellpadding=\"4\" style=\"border-collapse: collapse\">\n"
print "<th>Col1</th><th>Col2</th><th>Col3</th><th>Col4</th><th>Col5</th><th>Col6</th>"
print "<th>Col7</th><th>Col8</th><th>Col9</th><th>Col10</th><th>Col11</th>"
}
{
print "<tr>"
for( i = 1; i <= NF; i++ ) {
printf "%s", "<td"
if ($i+0>80) printf " bgcolor=#FF3333"
else if ($i+0>60) printf " bgcolor=#FFCC33"
else if ($i+0>30) printf " bgcolor=#99FF33"
print ">" $i "</td>"
}
print "</tr>"
}
END { printf "</table>\n</body>\n</html>\n" }' a.csv > 1.html
jimmyf
June 17, 2014, 7:21pm
5
Getting an error code Chub
nawk: syntax error at source line 1
context is
<<< BEGIN >>> {
nawk: illegal statement at source line 1
nawk: syntax error at source line 2
Strange this is working OK for me with gnu awk - how did you get the script onto your Solaris box did you edit it in windows and transfer. If so your file may be in DOS format.
Try using dos2unix your_script_filename to convert the file into unix format
jimmyf
June 17, 2014, 7:44pm
7
dos2unix
did it. Can this logic be applied to specific rows and columns ie
r=3, c=4
? Instead of numerical logic, can an instance of a cell that contains say the word "none" be targeted as green for example?
Sure you can do it like this, here NR is for rownumber; i is the column number :
nawk 'BEGIN {
FS=","
printf "<HTML>\n<body>\n<table border=\"1\" cellpadding=\"4\" style=\"border-collapse: collapse\">\n"
print "<th>Col1</th><th>Col2</th><th>Col3</th><th>Col4</th><th>Col5</th><th>Col6</th>"
print "<th>Col7</th><th>Col8</th><th>Col9</th><th>Col10</th><th>Col11</th>"
}
NR==3 {
print "<tr>"
for( i = 1; i <= NF; i++ ) {
printf "%s", "<td"
if (i==4 && $i=="none") printf " bgcolor=#99FF33"
else if (i==4 && $i+0 > 80) printf " bgcolor=#FFCC33"
else if (i==4 && $i+0 > 60) printf " bgcolor=#FF3333"
print ">" $i "</td>"
}
print "</tr>"
}
END { printf "</table>\n</body>\n</html>\n" }' a.csv > 1.html