HTML in unix shell scripting with sequential outpu

2019-09-06 09:18发布

I have a script called "main.ksh" which returns "output.txt" file and I am sending that file via mail.(list contains 50+ records, I just give 4 records for example)

mail output I am getting is:

DATE | FEED NAMEs | FILE NAMEs | JOB NAMEs | SCHEDULED_TIME| TIMESTAMP| SIZE(MB)| COUNT| STATUS |

Dec 17 INVEST_AI_FUNDS_FEED amlfunds_iai_20161217.txt gdcpl3392_uxmow080_ori_inv_ai TUE-SAT 02:03 0.4248 4031 On_Time

Dec 17 INVEST_AI_SECURITIES_FEED amltxn_iai_20161217.txt gdcpl3392_uxmow080_ori_inv_ai TUE-SAT 02:03 0.0015 9 On_Time

Dec 17 INVEST_AI_CONNECTED_PARTIES_FEED amlbene_iai_20161217.txt gdcpl3392_uxmow080_ori_inv_ai TUE-SAT 02:03 0.0001 1 No_Records

I am implementing coloring for Delayed,On_Time and No_Records field and I wrote below script which gives me bottom output(output is correct but there is no space separated).

awk 'BEGIN {
print "<html>" \
"<body bgcolor=\"#333\" text=\"#f3f3f3\">" \
"<pre>"
}

NR == 1 { print $0 }

NR > 1 {
if      ($NF == "Delayed")     color="red"
else if ($NF == "On_time")     color="green"
else if ($NF == "No_records")  color="yellow"
else                           color="#003abc"

$NF="<span style=\"color:" color "\">" $NF "</span>"

print $0
}

END {
print "</pre>" \
"</body>" \
"</html>"
}
' output.txt > output.html

output with perfect coloring:

| DATE | FEED NAMEs | FILE NAMEs | JOB NAMEs | SCHEDULED_TIME| TIMESTAMP| SIZE(MB)| COUNT| STATUS |

Dec 17 INVEST_AI_FUNDS_FEED amlfunds_iai_20161217.txt gdcpl3392_uxmow080_ori_inv_ai On_Time

Dec 17 INVEST_AI_SECURITIES_FEED amltxn_iai_20161217.txt gdcpl3392_uxmow080_ori_inv_ai On_Time

Dec 17 INVEST_AI_CONNECTED_PARTIES_FEED amlbene_iai_20161217.txt gdcpl3392_uxmow080_ori_inv_ai No_Records

There are 4 columns are skipped automatically. Could you please help me on this please ? Thanks a lot !

1条回答
霸刀☆藐视天下
2楼-- · 2019-09-06 09:33

When your code executes this

$NF="<span style=\"color:" color "\">" $NF "</span>"

print $0

the input line is rebuilt and therefore the multiple blanks between two consecutive fields are replaced by just ONE only blank space.

My solution copies the input line in a variable, deletes the last field (changing the value of the variable, not the input line), adds the modified last field and prints:

Dummy=$0
sub("[^ ]+$","",Dummy)   # removes last field
Dummy=Dummy "<span style=\"color:" color "\">" $NF "</span>"

print Dummy

Best regards

update: the last two code lines can be reduced in this way:

print Dummy "<span style=\"color:" color "\">" $NF "</span>"
查看更多
登录 后发表回答