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

时间:2015-01-13 14:29:45

标签: html bash loops for-loop tags

要添加像td / td等html标签(基本上是从输出中构建一个表格)我在我的for循环中使用sed:

for host in `cat hostlist`;do echo "$host " | sed 's|\(.*\)|<tr><td>\1</td>|'; cat $host | grep "CPU util" | grep " [5-9][0-9]%" | sed 's|\(.*\)|<td>\1</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返回0时),而且对于开关2,我希望所有的outut都在这样的一个表格单元格中:

期望的输出:

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

有什么建议吗? 感谢。

1 个答案:

答案 0 :(得分:0)

awk '/CPU util/{
    print "<td>" $0 "</tr>"
} 
!/CPU util/ {
    if ($1 == "")
        print "<tr><td>" $0 "</tr>"
    else 
        print "<tr><td>" $0 "</td>"
}' file_name`

会为你做的伎俩。自从我测试它并且它似乎起作用后,我决定回答而不是评论。