如何编写shell脚本从curl命令输出打印表格式

时间:2014-03-21 05:47:51

标签: bash shell curl sed awk

我想编写shell脚本,它可以从curl命令输出打印表格我的curl命令输出是

<table border='2'><tr><td>OSS Databse connectivity status </td><td> STATUS_OK</td></tr><tr><td>SharedMemCache connectivity status </td><td>STATUS_OK</td></tr><tr><td>OPX connectivity status </td><td>STATUS_OK</td></tr><tr><td>OMS connectivity status </td><td> STATUS_OK</td></tr><tr><td>Number of thread live </td><td>109</td></tr><tr><td>Number of thread waiting </td><td>34</td></tr><tr><td>Current state of Thread </td><td> RUNNABLE</td></tr><tr><td>OSS database latency </td><td>10 ms</td></tr><tr><td>OPX API latency </td><td>55 ms</td></tr><tr><td>OMS API latency </td><td>386 ms</td></tr><tr><td>Number of requests successfully processed in last 5 minutes : </td><td>0</td></tr></table>

我想在表格中安排此输出,如下所示

OSS Databse connectivity status  STATUS_OK
SharedMemCache connectivity status   STATUS_OK
OPX connectivity status          STATUS_OK
OMS connectivity status          STATUS_OK
Number of thread live            129
Number of thread waiting             39
Current state of Thread          RUNNABLE
OSS database latency             9 ms
OPX API latency                  2297 ms
OMS API latency                  342 ms
Number of requests successfully processed in last 5 minutes :   0

请帮帮我。

每次使用URL

点击curl时,状态都会更改

3 个答案:

答案 0 :(得分:1)

这个怎么样:

echo "data" | sed -r 's|<table[^>]*>(.*?)</table>|\1|g' | sed -r 's|<tr><td> *([^<]*)</td><td> *([^<]*)</td></tr>|\1^\2\n|g' | column -ts'^'

编辑:使用cygwin上的示例数据生成的输出:

OSS Databse connectivity status                                 STATUS_OK
SharedMemCache connectivity status                              STATUS_OK
OPX connectivity status                                         STATUS_OK
OMS connectivity status                                         STATUS_OK
Number of thread live                                           109
Number of thread waiting                                        34
Current state of Thread                                         RUNNABLE
OSS database latency                                            10 ms
OPX API latency                                                 55 ms
OMS API latency                                                 386 ms
Number of requests successfully processed in last 5 minutes :   0

答案 1 :(得分:1)

可以使用awk

awk '{gsub(/<\/td>/,"\t");gsub(/<\/tr>/,RS);gsub(/<[^>]*>/,"")}1' file
OSS Databse connectivity status          STATUS_OK
SharedMemCache connectivity status      STATUS_OK
OPX connectivity status         STATUS_OK
OMS connectivity status          STATUS_OK
Number of thread live   109
Number of thread waiting        34
Current state of Thread          RUNNABLE
OSS database latency    10 ms
OPX API latency         55 ms
OMS API latency         386 ms
Number of requests successfully processed in last 5 minutes :   0

如果你喜欢它格式更好:

awk '{gsub(/<\/td>/,"\t");gsub(/<\/tr>/,RS);gsub(/<[^>]*>/,"")}1' file | awk -F"\t" '{sub(/^ /,"",$2);printf "%-70s%s\n",$1,$2}'
OSS Databse connectivity status                                       STATUS_OK
SharedMemCache connectivity status                                    STATUS_OK
OPX connectivity status                                               STATUS_OK
OMS connectivity status                                               STATUS_OK
Number of thread live                                                 109
Number of thread waiting                                              34
Current state of Thread                                               RUNNABLE
OSS database latency                                                  10 ms
OPX API latency                                                       55 ms
OMS API latency                                                       386 ms
Number of requests successfully processed in last 5 minutes :         0

答案 2 :(得分:1)

以下是使用gnu-awk的另一种方式:

$ awk 'NR>1&&NF{++cnt;printf "%s%s",$0,ORS=cnt%2?" ":"\n"}' RS='</?t[rd]>|</?table>' file
OSS Databse connectivity status   STATUS_OK
SharedMemCache connectivity status  STATUS_OK
OPX connectivity status  STATUS_OK
OMS connectivity status   STATUS_OK
Number of thread live  109
Number of thread waiting  34
Current state of Thread   RUNNABLE
OSS database latency  10 ms
OPX API latency  55 ms
OMS API latency  386 ms
Number of requests successfully processed in last 5 minutes :  0