如何从shell脚本中的html表中提取数据? [英] How to extract data from html table in shell script?

查看:393
本文介绍了如何从shell脚本中的html表中提取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个BASH脚本,它将从HTML表中提取数据。
以下是我需要提取数据的表格示例:

 < table border = 1> 
< tr>
< td>< b>组件< / b>< / td>
< td>< b>状态< / b>< / td>
< td>< b>时间/错误< / b>< / td>
< / tr>
< tr>< td> SAVE_DOCUMENT< / td>< td>确定< / td>< td> 0.406 s< / td>< / tr>
< tr>< td> GET_DOCUMENT< / td>< td>确定< / td>< td> 0.332 s< / td>< / tr>
< tr>< td> DVK_SEND< / td>< td> OK< / td>< td> 0.001 s< / td>< / tr>
< tr>< td> DVK_RECEIVE< / td>< td>确定< / td>< td> 0.001 s< / td>< / tr>
< tr>< td> GET_USER_INFO< / td>< td> OK< / td>< td> 0.143 s< / td>< / tr>
< tr>< td>通知< / td>< td>确定< / td>< td> 0.001 s< / td>< / tr>
< tr>< td> ERROR_LOG< / td>< td> OK< / td>< td> 0.001 s< / td>< / tr>
< tr>< td> SUMMARY_STATUS< / td>< td>确定< / td>< td> 0.888 s< / td>< / tr>
< / table>

我想让BASH脚本像这样输出它:

  SAVE_DOCUMENT OK 0.475 s 
GET_DOCUMENT OK 0.345 s
DVK_SEND OK 0.002 s
DVK_RECEIVE OK 0.001 s
GET_USER_INFO OK 4.465 s
通知OK 0.001 s
ERROR_LOG OK 0.002 s
SUMMARY_STATUS OK 5.294 s

如何做到这一点?



到目前为止,我尝试过使用sed,但我不知道如何使用它。我使用 grep< tr>< td> 排除了使用grep排除的表头(组件,状态,时间/错误),因此只有以< tr>< td> 将被选择用于下一个解析(sed)。 ; \([^<>] [^<>] * \)GT; \([^<>] * \)LT / \1> @ \2 @ g'
但是,< tr> 标记仍然存在,也不会分开字符串,换句话说,脚本是:

 < tr> SAVE_DOCUMENTOK0.406 s< / tr> 

我正在处理的脚本的完整命令是:

  cat $ FILENAME | grep< tr>< td>| sed's @< \([^<>] [^<> * \)> \([^<>] * \)< / \ 1> @ \ 2 @ g'


(g)awk ,它有能力:-),这是一个解决方案,但是<解决方案 EM>请注意:它只能使用您发布的确切html表格格式。

  awk -F< / * td> |< / * tr>'/<\/*t[rd]>.*[AZ][AZ]/ {print $ 3,$ 5,$ 7}'FILE 

您可以在此看到它: https://ideone.com/zGfLe



一些解释:


  1. -F 将输入字段分隔符设置为正则表达式(任何 tr ' s或 td 的开始或结束标记

  2. 然后
  3. 只适用于与这些标记相匹配的行AND然后打印所需的字段。

HTH


I am trying to create a BASH script what would extract the data from HTML table. Below is the example of table from where I need to extract data:

<table border=1>
<tr>
<td><b>Component</b></td>
<td><b>Status</b></td>
<td><b>Time / Error</b></td>
</tr>
<tr><td>SAVE_DOCUMENT</td><td>OK</td><td>0.406 s</td></tr>
<tr><td>GET_DOCUMENT</td><td>OK</td><td>0.332 s</td></tr>
<tr><td>DVK_SEND</td><td>OK</td><td>0.001 s</td></tr>
<tr><td>DVK_RECEIVE</td><td>OK</td><td>0.001 s</td></tr>
<tr><td>GET_USER_INFO</td><td>OK</td><td>0.143 s</td></tr>
<tr><td>NOTIFICATIONS</td><td>OK</td><td>0.001 s</td></tr>
<tr><td>ERROR_LOG</td><td>OK</td><td>0.001 s</td></tr>
<tr><td>SUMMARY_STATUS</td><td>OK</td><td>0.888 s</td></tr>
</table>

And I want the BASH script to output it like so:

SAVE_DOCUMENT OK 0.475 s
GET_DOCUMENT OK 0.345 s
DVK_SEND OK 0.002 s
DVK_RECEIVE OK 0.001 s
GET_USER_INFO OK 4.465 s
NOTIFICATIONS OK 0.001 s
ERROR_LOG OK 0.002 s
SUMMARY_STATUS OK 5.294 s

How to do it?

So far I have tried using the sed, but I don't know how to use it quite well. The header of the table(Component, Status, Time/Error) I excluded with grep using grep "<tr><td>, so only lines starting with <tr><td> will be selected for next parsing (sed). This is what I used: sed 's@<\([^<>][^<>]*\)>\([^<>]*\)</\1>@\2@g' But then <tr> tags still remain and also it wont separate the strings. In other words the result of this script is:

<tr>SAVE_DOCUMENTOK0.406 s</tr>

The full command of the script I'm working on is:

cat $FILENAME | grep "<tr><td>" | sed 's@<\([^<>][^<>]*\)>\([^<>]*\)</\1>@\2@g'

解决方案

Go with (g)awk, it's capable :-), here is a solution, but please note: it's only working with the exact html table format you had posted.

 awk -F "</*td>|</*tr>" '/<\/*t[rd]>.*[A-Z][A-Z]/ {print $3, $5, $7 }' FILE

Here you can see it in action: https://ideone.com/zGfLe

Some explanation:

  1. -F sets the input field separator to a regexp (any of tr's or td's opening or closing tag

  2. then works only on lines that matches those tags AND at least two upercasse fields

  3. then prints the needed fields.

HTH

这篇关于如何从shell脚本中的html表中提取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆