如何在Shell脚本中使用echo命令以HTML格式打印新行

时间:2018-08-30 14:08:34

标签: html css shell

这是我的要求。

$ cat 123.log

ebspdv1START
Patch description:  "OJVM PATCH SET UPDATE 11.2.0.4.171017"
Patch description:  "OCW PATCH SET UPDATE: 11.2.0.4.180417 (27441052)"
Patch description:  "DATABASE PATCH FOR EXADATA (Apr 2018 - 11.2.0.4.180417) : (27346006)"
ebspdv1END

$ Iname=ebspdv1
$ ora_inst_home=/bio/vcp/ppt/db/11.2.0.4
echo "<TR><TD class='dbcc'>Main Patch info of <B>$Iname: $ora_inst_home</B></TD><TD class='dbcnc'>`cat 123.log |sed -n "/^${Iname}START\$/,/^${Iname}END\$/p"|grep -v $Iname|cut -d: -f2-3|sed 's/\"//g'`</TD></TR>" >>123.html

以HTML格式显示当前输出:

Main Patch info of ebspdv1: /bio/vcp/ppt/db/11.2.0.4    OJVM PATCH SET UPDATE 11.2.0.4.171017 OCW PATCH SET UPDATE: 11.2.0.4.180417 (27441052) DATABASE PATCH FOR EXADATA (Apr 2018 - 11.2.0.4.180417) : (27346006)

HTML的预期输出(每个值应在新行中打印):

Main Patch info of ebspdv1: /bio/vcp/ppt/db/11.2.0.4    OJVM PATCH SET UPDATE 11.2.0.4.171017 
                                                        OCW PATCH SET UPDATE: 11.2.0.4.180417 (27441052) 
                                                        DATABASE PATCH FOR EXADATA (Apr 2018 - 11.2.0.4.180417) : (27346006)

当前HTML输出:

Present HTML Output screenshot

1 个答案:

答案 0 :(得分:0)

提取补丁信息的管道可以完全在一个sed调用中编写:

sed -n "/^${Iname}START\$/,/^${Iname}END\$/ {
    /^$Iname/d
    s/^[^\"]\+\"//
    s/\"$/<br\/>/
    p
}" 123.log

或awk,根据您的喜好可能更具可读性:

awk -v iname="$Iname" '
    BEGIN                {FS = "\""; ORS = "<BR/>\n"}
    $0 ~ "^"iname"END"   {p = 0}
    p                    {print $2}
    $0 ~ "^"iname"START" {p = 1}
' 123.log

然后,出于可读性考虑,我将HTML放在Heredoc中:

patch_info=$(
    sed -n "/^${Iname}START\$/,/^${Iname}END\$/ {
        /$Iname/d
        s/^[^\"]\+\"//
        s/\"$/<br\/>/
        p
    }" 123.log
)
cat <<HTML >>123.html
<TR>
<TD class='dbcc'>Main Patch info of <B>$Iname: $ora_inst_home</B></TD>
<TD class='dbcnc'>$patch_info</TD>
</TR>
HTML