【问题标题】:Assigning colors to HTML table columns based on conditions根据条件为 HTML 表格列分配颜色
【发布时间】:2019-12-18 12:23:00
【问题描述】:

我正在使用 bash 脚本以 html table 格式发送电子邮件。

我有一个类似下面的命令。

列是:

table1_amount
table1_count
table2_amount
table2_count
count_validation
amount_validation

场景:

当计数匹配时,count_validation 列将与绿色背景“匹配”,否则为红色背景。金额的方式相同。

我的代码中遗漏了什么或做错了什么?

使用下面的语句,我可以为 count_validation 但不能为 amount_validation 列。

awk -F"#" 'BEGIN{print "<style> body { background-color: #FFF;text-align:left; } table { font-family:Tahoma;font-size:11;border-collapse:collapse; border: 4px solid #dddddd;} th { padding:10px;background-color: #94E1F7; } th { border: 1px solid black; } td { padding:3px;border: 2px solid black;text-align:right; } </style></head><body><table>  <tr> <th>table1_count</th> <th>table1_amount</th> <th>table2_count</th> <th>table2_amount</th> <th>count_validation</th> <th>amount_validation</th></tr>"} {print "<tr>";for(i=1;i<=NF;i++) if (i=="5"){ if ($i=="Matching") {print "<td bgcolor='#4dffa6' >" $i"</td>"} else {print "<td bgcolor='red'>" $i"</td>"} elif (i=="6"){ if ($i=="Matching") {print "<td bgcolor='green' >" $i"</td>"} else {print "<td bgcolor='red'>" $i"</td>"}} else {print "<td>" $i "</td>"} ;print "</tr>"} END{print "</table>"}' ${tmp_dir}/QC_VALIDATION_REPORT.txt >> ${tmp_dir}/QC_VALIDATION_REPORT.html

awk -F"#" 'BEGIN{print" body { background-color: #FFF;text-align:left; } table { font-family:Tahoma;font-size:11;border-collapse:collapse;边框:4px 纯色#dddddd;} th { padding:10px;background-color: #94E1F7; } th { 边框:1px 纯黑色; } td { padding:3px;边框:2px 纯黑色;text-align:right; } table1_count table1_amount table2_count table2_amount count_validation amount_validation"} {print "";for(i=1;i> ${tmp_dir}/QC_VALIDATION_REPORT.html

Dummy data

718#394682876.71#718#394682876.71#Matching#Matching
30956#6637761.58#30956#6637760.58#Matching#Not_Matching

【问题讨论】:

  • 你的 awk 命令真的像这样一行吗?如果你能把它分成多行,我会更容易阅读。为了便于阅读,我编辑了您帖子的其余部分,但我不想编辑 awk 行以防我破坏它,但如果您可以编辑它以将其放在多行上,它将帮助读者理解您的题。您可能还想显示它产生的输出以及您想要的输出,以帮助我们进一步了解。
  • 我不认为awk中有elif
  • @Samha' 我已经添加了elif,但是这个没有给我正确的结果
  • 还有一个结束&lt;/head&gt;标签和一个开始&lt;body&gt;,可以吗?
  • 你能添加一个带有虚拟数据的示例文件来正确测试吗?

标签: html linux bash shell


【解决方案1】:

以下是您提供的虚拟数据代码作为简单测试:

data="718#394682876.71#718#394682876.71#Matching#Matching
30956#6637761.58#30956#6637760.58#Matching#Not_Matching"


awk -F"#" '
  BEGIN {
    print "<html><head>\
      <style>\
        body {\
          background-color: #FFF;text-align:left;\
        }\
        table {\
          font-family:Tahoma;\
          font-size:11;\
          border-collapse:collapse;\
          border: 4px solid #dddddd;\
        }\
        th { padding:10px;\
          background-color: #94E1F7;\
        }\
        th {\
          border: 1px solid black;\
        }\
        td {\
          padding:3px;\
          border: 2px solid black;\
          text-align:right;\
        }\
      </style>\
    </head>\
    <body>\
      <table>\
        <tr>\
          <th>table1_count</th>\
          <th>table1_amount</th>\
          <th>table2_count</th>\
          <th>table2_amount</th>\
          <th>count_validation</th>\
          <th>amount_validation</th>\
        </tr>"
  }
  {
    print "<tr>"
    printf "<td>%s</td><td>%s</td><td>%s</td><td>%s</td>",$1,$2,$3,$4
    for (i = 5; i < 7; i++) {
      if ($i == "Matching") {
        printf "<td bgcolor=\"green\">%s</td>",$i
      } else {
        printf "<td bgcolor=\"red\">%s</td>",$i
      }
    }
    print "</tr>"
  }
  END {
    print "</table></body></html>"
  }
' <(echo "$data") > ./test.html

在这里,关于您发布的代码的一些注释:

  1. elifawkelse if 中无效,如果你真的需要这个,你必须嵌套if 条件
  2. 保留缩进,它确实有助于可读性,这被认为是干净代码最重要的方面。
  3. 尽量避免嵌套条件。超过 2 级 是不好的。

【讨论】:

  • 作为一个魅力。这就是我一直在寻找的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-13
  • 1970-01-01
  • 1970-01-01
  • 2019-04-09
  • 2017-05-20
相关资源
最近更新 更多