HTML标签的AWK脚本编制问题 [英] Issues with AWK scripting for HTML tags
问题描述
下面是数据文件(结果)的内容-
Below is the data file(results) contents-
13450708,13470474,US,15
24954,24845,JPN,44
14258992,14365059,US,4
24954,24845,IND,44
我想将以上数据集以表格格式发送到电子邮件.为此,我正在使用以下awk脚本. 现在我在这里面临的挑战是-如果数据集中的最后一个字段(即此处15,44,4,44)> 40,我想将背景色设为红色. 您能告诉我如何在下面的代码中使用它吗?
I want to send above data sets to email in a tabular format. For that I am using below awk script. Now the challenge I am facing here is - I want to make the background color as red if the lastfield in the datasets ( i.e. here 15,44,4,44) > 40. Can you please tell me how to use the same in below code.
awk 'BEGIN{
FS=","
print "<HTML>""<TABLE border="1"><TH>Store_count</TH><TH>Store_sold</TH><TH>Store_code</TH><TH>Backlogs</TH>"
}
{
printf "<TR>"
for(i=1;i<=NF;i++)
printf "<TD>%s</TD>", $i
print "</TR>"
}
END{
print "</TABLE></BODY></HTML>"
}
' results > file1.html
推荐答案
我真的不明白您为什么要为此苦苦挣扎,因为您似乎已经掌握了所有信息,可以做自己想做的事情,但是无论如何-只是更改:
I really don't understand why you're struggling with this since you seem to already have all of the information to do what you want, but anyway - just change:
printf "<TD>%s</TD>", $i
到
printf "<TD%s>%s</TD>", ( (i==NF) && ($i > 40) ? " style=\"background-color:red\"" : "" ), $i
或者如果您不喜欢三元表达式:
or if you don't like ternary expressions:
printf "<TD"
if ( (i==NF) && ($i > 40) ) {
printf " style=\"background-color:red\""
}
printf ">%s</TD>, $i
或类似的
这篇关于HTML标签的AWK脚本编制问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!