如何通过awk命令实现着色并通过邮件发送?

时间:2017-01-10 04:00:11

标签: html shell unix awk ksh

我正在尝试将.txt文件转换为.html文件以进行着色并通过邮件发送。 我正在获得适当的着色,直到" MQ-FEEDs:"如下面的例子所述。 但剩下的选定案例会被打印两次,第二次被着色(例如:FAILED FAILED(红色)),请你帮我解决这个问题,非常感谢!

我得到的输出:

报告:

|DATE | FEED NAMEs AS OF NOW          | AVG_TIME| ARR_TIME |SIZE(MB)|COUNT|STATUS |  
Jan  6 CHCK09_20170106.txt                20:46     21:15   0.180   1430    DELAYED
Jan  6 EGLOUT_xml_20170106.txt            15:46     15:46   0.008   247     On_Time   
Jan  9 MTS_SOD_20170106.TXT               07:05     00:06   4.005   1       No_Records    
MQ-FEEDs:    
Jan 09 03:39  MTS_TRANSACTION_FEED      MQ    1   3719238761373 SUCCESS SUCCESS    
Jan 09 03:37  HOGAN_TRANSACTION_FEED    MQ    1   3719235506904 FAILED  FAILED    
MANUAL-UPLOAD-FEEDs:                                                             MANUAL-UPLOAD-FEEDs:   
DB-POOL-FEEDs:                                                                   DB-POOL-FEEDs:    
NOTE : Feednames starting with PLUS sign(+) indicates they are in INPROGRESS Directory.   
   DELAYING-FILES-ARE                                                DELAYING-FILES-ARE

代码:

#!/bin/ksh
export PATH1="/auto/users/scripts"

awk 'BEGIN {
  print "<html>" \
    "<body bgcolor=\"#DEF3F0\" text=\"#003abc\">" \
    "<pre>"
}

NR == 0 { print $0 }
NR > 0 {
  if      ($NF == "DELAYED")     {color="red"; bold=1; size="15px"; italic=0;}
  else if ($NF == "On_Time")     {color="green"; bold=1; size="15px"; italic=0;}
  else if ($NF == "SUCCESS")     {color="green"; bold=1; size="15px"; italic=0;}
  else if ($NF == "FAILED")     {color="red"; bold=1; size="15px"; italic=0;}
  else if ($NF == "MQ-FEEDs:")     {color="magenta"; bold=1; size="15px"; italic=0;}
  else if ($NF == "MANUAL-UPLOAD-FEEDs:")     {color="magenta"; bold=1; size="15px"; italic=0;}
  else if ($NF == "DB-POOL-FEEDs:")     {color="magenta"; bold=1; size="15px"; italic=0;}
  else if ($NF == "No_Records")  {color="blueviolet"; bold=0; size="20px"; italic=1;}
  else if ($NF == "DELAYING-FILES-ARE")           {color="red"; bold=1; size="25px"; italic=0;}
  else                           {color="#003abc"; bold=0; size="25px"; italic=0;}

  Dummy=$0
  sub("[^ ]+$","",Dummy)
  print Dummy "<span style=\"color:" color (bold ? ";font-weight:bold" : "")(size ? ";font-size:size" : "") (italic ? ";font-style:italic" : "") "\">" $NF "</span>"
}

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

mail -s "$(echo -e "This is Subject\nContent-Type: text/html")"  xyz@email.com  <  output.html

1 个答案:

答案 0 :(得分:1)

您需要对输出加倍进行调整。

  LASTFIELD=$NF      
  Dummy=$0
  gsub("[\t ]+$","",Dummy)
  gsub("[^ ]+$","",Dummy)
  print Dummy"<span style=\"color:" color (bold ? ";font-weight:bold" : "")(size ? ";font-size:size" : "") (italic ? ";font-style:italic" : "") "\">"LASTFIELD"</span>"