【问题标题】:How to implement coloring through awk command and send it via mail?如何通过awk命令实现着色并通过邮件发送?
【发布时间】:2017-01-10 04:00:11
【问题描述】:

我正在尝试将 .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

【问题讨论】:

  • @Jdamian,您好,先生,这个问题就像另一个问题,但这里的要求有点不同。
  • @Jdamian,你能帮我解决这个问题吗?

标签: html shell unix awk ksh


【解决方案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>"

【讨论】:

  • 嗨,先生,它对我有用,但它正在删除空格(不是按顺序),所以我没有在特定的空间间隙中得到我的输出。你能帮我解决这个问题吗?
  • 编辑了我的答案,它将以相同的间距显示。
猜你喜欢
  • 2011-01-17
  • 2016-02-21
  • 2016-09-24
  • 2011-01-23
  • 2013-02-25
  • 2015-04-05
  • 2019-12-18
  • 2010-11-20
  • 2014-06-05
相关资源
最近更新 更多