带有顺序输出的unix shell脚本中的HTML [英] HTML in unix shell scripting with sequential output

查看:220
本文介绍了带有顺序输出的unix shell脚本中的HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为main.ksh的脚本,它返回output.txt文件,我通过邮件发送该文件(列表包含50多条记录,我只给出4条记录)

b
$ b

邮件输出我得到的是:

DATE | FEED NAMEs |文件名称|工作职位名称| SCHEDULED_TIME | TIMESTAMP |大小(MB)| COUNT |状态|



12月17 INVEST_AI_FUNDS_FEED amlfunds_iai_20161217.txt gdcpl3392_uxmow080_ori_inv_ai TUE-SAT 02:03 0.4248 4031 ON_TIME



12月17日INVEST_AI_SECURITIES_FEED amltxn_iai_20161217.txt gdcpl3392_uxmow080_ori_inv_ai TUE-SAT 02:03 0.0015 9 On_Time

12月17日INVEST_AI_CONNECTED_PARTIES_FEED amlbene_iai_20161217.txt gdcpl3392_uxmow080_ori_inv_ai TUE-SAT 02:03 0.0001 1 No_Records



我正在为Delayed,On_Time和No_Records字段实现着色,并且我在脚本的下面写了底部输出(输出正确但没有空格分隔)。

  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$ b $ 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

完美填色输出:

| DATE | FEED NAMEs |文件名称|工作职位名称| SCHEDULED_TIME | TIMESTAMP |大小(MB)| COUNT |状态|



12月17日INVEST_AI_FUNDS_FEED amlfunds_iai_20161217.txt gdcpl3392_uxmow080_ori_inv_ai On_Time
$ b

12月17日INVEST_AI_SECURITIES_FEED amltxn_iai_20161217.txt gdcpl3392_uxmow080_ori_inv_ai On_Time

12月17日INVEST_AI_CONNECTED_PARTIES_FEED amlbene_iai_20161217.txt gdcpl3392_uxmow080_ori_inv_ai No_Records



有4列自动跳过。你能帮我解决这个问题吗?

解决方案

当您的代码执行此操作时,

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

print $ 0

输入行被重建,因此多个空格两个连续的字段被替换为只有一个空格



我的解决方案将输入行复制到一个变量中,删除最后一个字段

  Dummy = $ 0 $ b $(变量的值,而不是输入行)添加修改的最后一个字段并打印: b sub([^] + $,,Dummy)#删除最后一个字段
Dummy = Dummy< span style = \color:color\> $ NF< / span>

打印假币

最好的祝愿

更新:最后两条代码行可以用这种方式减少:

  print Dummy< span style = \color:color\> $ NF< / span> 


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

这篇关于带有顺序输出的unix shell脚本中的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆