bash 循环脚本,在每个输出上添加 html 标签

bash script for loop, add html tag on each output

要添加 html 标签,如 td /td 等(基本上是从输出构建 table),我在我的 for 循环中使用 sed:

for host in `cat hostlist`;do echo "$host " | sed 's|\(.*\)|<tr><td></td>|'; cat $host | grep "CPU util" | grep " [5-9][0-9]%" | sed 's|\(.*\)|<td></td></tr>|'; done

输出没有sed:

switch1

switch2
Core 0: CPU utilization for five seconds: 56%; one minute: 11%; five minutes: 8%
Core 0: CPU utilization for five seconds: 56%; one minute: 11%; five minutes: 8%
CPU utilization for five seconds: 63%; one minute: 23%;  five minutes: 18%
switch3
CPU utilization for five seconds: 78%/0%; one minute: 43%; five minutes: 40%

sed 输出:

<tr><td>switch1 </td>
<tr><td>switch2 </td>
<td>Core 0: CPU utilization for five seconds: 56%; one minute: 11%; five minutes: 8%</td></tr>
<td>Core 0: CPU utilization for five seconds: 56%; one minute: 11%; five minutes: 8%</td></tr>
<td>CPU utilization for five seconds: 63%; one minute: 23%;  five minutes: 18%</td></tr>
<tr><td>switch3 </td>
<td>CPU utilization for five seconds: 78%/0%; one minute: 43%; five minutes: 40%</td></tr>

这不会将标签添加到空行,例如 switch1 下面的行(当 grep returns 0 时),对于 switch 2 我希望所有输出都在一个 table 单元格中,就像这样:

期望的输出:

<tr><td>switch1 </td>
<td></td></tr>
<tr><td>switch2 </td>
<td>Core 0: CPU utilization for five seconds: 56%; one minute: 11%; five minutes: 8%<brZ
Core 0: CPU utilization for five seconds: 56%; one minute: 11%; five minutes: 8%<br>
CPU utilization for five seconds: 63%; one minute: 23%;  five minutes: 18%</td></tr>
<tr><td>switch3 </td>
<td>CPU utilization for five seconds: 78%/0%; one minute: 43%; five minutes: 40%</td></tr>

有什么建议吗? 谢谢。

awk '/CPU util/{
    print "<td>" [=10=] "</tr>"
} 
!/CPU util/ {
    if ( == "")
        print "<tr><td>" [=10=] "</tr>"
    else 
        print "<tr><td>" [=10=] "</td>"
}' file_name`

会帮你解决问题的。因为我测试了它并且它似乎有效,所以我决定回答而不是评论。