Skip to content
Advertisement

Assigning colors to HTML table columns based on conditions

I am using a bash script to send an email in html table format.

I have a command like below.

Columns are:

table1_amount
table1_count
table2_amount
table2_count
count_validation
amount_validation

scenario:

When counts are matching then count_validation column will have ‘Matching’ with green color background else red color background. Same way for amounts.

What am I missing or doing wrong in my code?

Using the below statement I am able to do for count_validation but not for amount_validation column.

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; 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; } table1_count table1_amount table2_count table2_amount count_validation amount_validation”} {print “”;for(i=1;i<=NF;i++) if (i==”5″){ if ($i==”Matching”) {print “” $i””} else {print “” $i””} elif (i==”6″){ if ($i==”Matching”) {print “” $i””} else {print “” $i””}} else {print “” $i “”} ;print “”} END{print “”}’ ${run_project_tmp_dir}/QC_VALIDATION_REPORT.txt >> ${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

Advertisement

Answer

Here is the code with the dummy data you provided as a simple test:

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

And here, some notes on your posted code:

  1. elif isn’t valid in awk, nor else if, if you really need this, you’ll have to nest if conditions.
  2. preserve indentation, it really helps in terms of readability which is considered the most important aspect of clean code.
  3. avoid nesting conditionals as much as you can. More than 2-levels is bad.
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement