【问题标题】:How to write shell sript to print table format from curl command output如何编写 shell 脚本以从 curl 命令输出中打印表格格式
【发布时间】:2014-04-28 08:56:45
【问题描述】:

我想编写可以从curl 命令输出打印表格的shell 脚本我的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

请帮帮我。

每次我使用curl点击URL时,状态都会改变

【问题讨论】:

  • 大家好,感谢您的回答都很有用..

标签: bash shell curl sed awk


【解决方案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

【讨论】:

  • 嗯,从未在 awk 中编程过。看起来很整洁。您也不必剪掉table 标签...
  • @PlasmaPower awk 是一个很棒的数据处理工具。 OP 请求未在输出中显示 &lt;table&gt; 标记,因此已将其删除。
【解决方案2】:

这是使用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

【讨论】:

  • 您可以使用printf "%-62s%s" 进行更清晰的格式化。
【解决方案3】:

这个怎么样:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    相关资源
    最近更新 更多