根据HTML表中的条件更改列颜色

时间:2019-08-30 14:44:21

标签: html linux bash shell if-statement

我正在尝试使用bash格式的HTML脚本发送电子邮件。

列名在下面

Portfolio
account_number
critical
distinct_file_name_count
total_file_count
Files_validation

AWK命令,用于填充.HTML文件

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>Portfolio</th> 
                                    <th>account_number</th> 
                                    <th>critical</th> 
                                    <th>distinct_file_name_count</th> 
                                    <th>total_file_count</th> 
                                    <th>Files_validation</th> 
                                </tr>" 
} 
{ 
    print "<tr>" 
    printf "<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>",$1,$2,$3,$4,$5 
    for (i = 6; i < 7; i++) { 
        if ($i == "Files_not_received_today") { 
            printf "<td bgcolor=#FF0000>%s</td>",$i 
        } else { 
            printf "<td bgcolor=#32CD32>%s</td>",$i 
        }
    } print "</tr>" 
} END { 
    print "</table></body></html>"
}' ${run_project_tmp_dir}/QC_VALIDATION_REPORT.txt >> ${run_project_tmp_dir}/QC_VALIDATION_REPORT.html

我要进行更改的条件是

1) When column when `Files_validation` column is having `Files_not_received_today` then check if `critical` column is `YES`. 

If `YES` then Populate `RED` color in both `Files_validation` and `critical`. Else if  when `Files_validation` column is having `Files_not_received_today` then check if `critical` column is `NO` then Populate `AMBER` color in both `Files_validation` and `critical`.

2)  when `Files_validation` column is having other than `Files_not_received_today` Populate `GREEN` color in `Files_validation`

使用上面的awk命令,我可以根据我的期望用Files_validationRED颜色填充GREEN,但无法达到我期望的条件结果提到

Dummy data

TESTER#394682876#YES#2#23#Files_received_today
BILLER#6637761#NO#0#0#Files_not_received_today
APPROVER#23478#YES#1#1#Files_received_today
BUYER#12398#YES#0#0#Files_not_received_today

current output

TESTER#394682876#YES#2#23#Files_received_today  == No bgcolor in critical and bgcolor of files_validation column is Green
BILLER#6637761#NO#0#0#Files_not_received_today  == No bgcolor in critical and bgcolor of files_validation column is Red
APPROVER#23478#YES#1#1#Files_received_today     == No bgcolor in critical and bgcolor of files_validation column is Green
BUYER#12398#YES#0#0#Files_not_received_today    == No bgcolor in critical and bgcolor of files_validation column is Red 

expected output

TESTER#394682876#YES#2#23#Files_received_today  == No bgcolor in critical and bgcolor of files_validation column is Green
BILLER#6637761#NO#0#0#Files_not_received_today  == AMBER bgcolor in critical and bgcolor of files_validation column is AMBER
APPROVER#23478#YES#1#1#Files_received_today     == No bgcolor in critical and bgcolor of files_validation column is Green
BUYER#12398#YES#0#0#Files_not_received_today    == RED bgcolor in critical and bgcolor of files_validation column is Red 

1 个答案:

答案 0 :(得分:1)

在从@nmr追加输入之前,我将假设问题在于如何在awk中编写必要的代码以允许分配3种不同的颜色:

RED   = FF0000
AMBER = FFFB00     # per https://www.colorhexa.com/ffbf00
GREEN = 32CD32

有很多方法可以执行此操作... case / switch语句,if / then / else嵌套等。

我要用4个命令替换当前的for/if/then/else/printf块;前3个命令将确定bgcolor,而第4个命令将是新的printf命令。

注意:BEGINEND块将保持不变。

3个确定bgcolor的命令:

# set the default color to GREEN for each new line of input data

bgcolor="#32CD32"

# if  field #6 is the string "Files_not_received_today"
# and field #3 = 'YES' then (re)set color to RED

if ( $6 == "Files_not_received_today" && $3 == "YES" ) { bgcolor="#FF0000" } 

# if  field #6 is the string "Files_not_received_today"
# and field #3 = 'NO' then (re)set color to AMBER

if ( $6 == "Files_not_received_today" && $3 == "NO"  ) { bgcolor="#FFBF00" }

新的打印声明:

printf "<td bgcolor="bgcolor">%s</td>",$6

将所有这些awk代码的中间块组合在一起:

{
    print "<tr>"
    printf "<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>",$1,$2,$3,$4,$5

    bgcolor="#32CD32"                                                            # GREEN
    if ( $6 == "Files_not_received_today" && $3 == "YES" ) { bgcolor="#FF0000" } # RED
    if ( $6 == "Files_not_received_today" && $3 == "NO"  ) { bgcolor="#FFBF00" } # AMBER

    printf "<td bgcolor="bgcolor">%s</td>",$6
    print "</tr>"
} 

当对示例4x数据行运行时,我们为表单元格获取以下html代码:

<tr>
<td>TESTER</td><td>394682876</td><td>YES</td><td>2</td><td>23</td><td bgcolor=#32CD32>Files_received_today</td></tr>
<tr>
<td>BILLER</td><td>6637761</td><td>NO</td><td>0</td><td>0</td><td bgcolor=#FFBF00>Files_not_received_today</td></tr>
<tr>
<td>APPROVER</td><td>23478</td><td>YES</td><td>1</td><td>1</td><td bgcolor=#32CD32>Files_received_today</td></tr>
<tr>
<td>BUYER</td><td>12398</td><td>YES</td><td>0</td><td>0</td><td bgcolor=#FF0000>Files_not_received_today</td></tr>

当将完整的html块加载到浏览器中时,我们得到:

enter image description here