谁能帮我解决这个问题吗?我有一个 CSV 文件,其中包含如下文本。我可以从 CSV 文件创建一个 HTML 文件,但无法更改文本颜色,如成功则应为绿色,所有失败则应为红色。
CSV 文件内容
HB load,14-Jun-2018, success
MF load,14-Jun-2018,failure
SP load,14-Jun-2018,success
LN load,14-Jun-2018,failure
脚本
awk 'BEGIN{
FS=","
print "<HTML>""<TABLE border="1"><TH>JOB_NAME</TH><TH>RUN_DATE</TH><TH>STATUS</TH>"
}
{
printf "<TR>"
for(i=1;i<=NF;i++)
printf "<TD>%s</TD>", $i
print "</TR>"
}
END{
print "</TABLE></BODY></HTML>"
}
' test > file.html
(
echo "To: [email protected]"
echo "Subject: Report"
echo "Content-Type: text/html"
echo
cat file.html
echo
) | /usr/sbin/sendmail -t
file.html 内容
<HTML><TABLE border=1><TH>JOB_NAME</TH><TH>RUN_DATE</TH><TH>STATUS</TH>
<TR><TD>HB load</TD><TD>14-Jun-2018</TD><TD>success </TD></TR>
<TR><TD>MF load</TD><TD>14-Jun-2018</TD><TD>failure</TD></TR>
<TR><TD>SP load</TD><TD>14-Jun-2018</TD><TD>success</TD></TR>
<TR><TD>LN load</TD><TD>14-Jun-2018</TD><TD>failure</TD></TR>
</TABLE></BODY></HTML>
答案1
尝试这个,
awk 'BEGIN{
FS=","
print "<HTML>""<TABLE border="1"><TH>JOB_NAME</TH><TH>RUN_DATE</TH><TH>STATUS</TH>"
}
{
printf "<TR>"
for(i=1;i<=NF;i++)
printf "<TD>%s</TD>", $i
print "</TR>"
}
END{
print "</TABLE></BODY></HTML>"
}
' test > file.html
sed -i "s/failure/<font color="red">failure<\/font>/g;s/success/<font color="green">success<\/font>/g" file.html
(
echo "To: [email protected]"
echo "Subject: Report"
echo "Content-Type: text/html"
echo
cat file.html
echo
) | /usr/sbin/sendmail -t