Bash 在shell脚本中的标记之间提取数据并存储在数组中?

Bash 在shell脚本中的标记之间提取数据并存储在数组中?,bash,shell,Bash,Shell,我使用bash脚本从URL获取一个值,它以html标签的形式返回一个值。输入: <tr><td title='The name of the health check service.'>hc.name</td><td data-type='java.lang.String'>Replication Queue</td></tr> <tr><td title='The persistence identif

我使用
bash
脚本从URL获取一个值,它以html标签的形式返回一个值。输入:

<tr><td title='The name of the health check service.'>hc.name</td><td data-type='java.lang.String'>Replication Queue</td></tr>
<tr><td title='The persistence identifier of the service.'>service.pid</td><td data-type='java.lang.String'>com.adobe.granite.replication.hc.impl.ReplicationQueueHealthCheck</td></tr>
<tr><td title='The health check result'>ok</td><td data-type='java.lang.Boolean'>true</td></tr>
<tr><td title='The health check status'>status</td><td data-type='java.lang.String'>OK</td></tr>
<tr><td title='The elapsed time in miliseconds'>elapsedTime</td><td data-type='java.lang.Long'>18</td></tr>
<tr><td title='The date when the execution finished'>finishedAt</td><td data-type='java.util.Date'>2017-03-24T00:23:36+0530</td></tr>
<tr><td title='Indicates of the execution timed out'>timedOut</td><td data-type='java.lang.Boolean'>false</td></tr>
我尝试使用这个
sed
代码,但它只在多个
标记位于不同的行时才起作用。在这种情况下,多个
在同一行上

sed -n 's:.*<td>(.*)</td>.*:\1:p' sample.txt 
sed-n的:.*(.*):\1:p'sample.txt
该命令仅使用以下输入显示结果:

<tr>
<td>ok</td>
<td>status</td>
</tr>

好啊
地位

我认为使用Perl正则表达式会更幸运,因为它们支持非贪婪匹配。下面是一个Perl one liner,用于打印文件中的信息:

perl -ne 'm:.*?<td [^>]*>(.*?)</td>.*?<td [^>]*>(.*?)</td>:; print "[\"$1\"] = \"$2\"\n";' sample.txt

这里有一个sed调用也可以使用,但它不太精确,因为它匹配所有字符,除了
sgrep
sed
方法(比纯
sed
更可靠):


问题是什么?我不熟悉Shell脚本,我尝试了一些使用sed/awk的命令,但它不起作用,我也不知道如何将其填充为上面提到的数组结构。您使用过任何Shell脚本/命令吗?请描述这个问题,并告诉我们到目前为止已经做了什么来解决它。sed-n的:.*(.*):\1:p'sample.txt我尝试使用这个命令,但当多个标记在单独的行中时,它可以工作。但是,在我的例子中,多个在同一行中。这里通常的答案是“不要使用正则表达式来解析HTML”。最佳实践方法更像是使用
xmltidy
将HTML转换为XML,然后使用XMLStarlet将XML解析为文本。
perl -ne 'm:.*?<td [^>]*>(.*?)</td>.*?<td [^>]*>(.*?)</td>:; print "[\"$1\"] = \"$2\"\n";' sample.txt
["hc.name"] = "Replication Queue"
["service.pid"] = "com.adobe.granite.replication.hc.impl.ReplicationQueueHealthCheck"
["ok"] = "true"
["status"] = "OK"
["elapsedTime"] = "18"
["finishedAt"] = "2017-03-24T00:23:36+0530"
["timedOut"] = "false"
sed -n 's:.*<td [^>]*>\([^<]*\)</td><td [^>]*>\([^<]*\)</td>.*:[\"\1\"] = \"\2\":p' sample.txt
["hc.name"] = "Replication Queue"
["service.pid"] = "com.adobe.granite.replication.hc.impl.ReplicationQueueHealthCheck"
["ok"] = "true"
["status"] = "OK"
["elapsedTime"] = "18"
["finishedAt"] = "2017-03-24T00:23:36+0530"
["timedOut"] = "false"
sgrep -o'%r"' '">" __ "<"' sample.txt | sed 's/^/["/;s/""/"/;s/"/"]="/2'
["hc.name"]="Replication Queue"
["service.pid"]="com.adobe.granite.replication.hc.impl.ReplicationQueueHealthCheck"
["ok"]="true"
["status"]="OK"
["elapsedTime"]="18"
["finishedAt"]="2017-03-24T00:23:36+0530"
["timedOut"]="false"