简体   繁体   中英

HTML in unix shell scripting with sequential output

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 !

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>"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM