【发布时间】:2017-12-13 04:40:20
【问题描述】:
我有一个问题。我想使用 sed 或 grep 命令在值中获取此 html 的两个部分。我怎样才能把它们都提取出来?
test.html:
<html>
<body>
<div id="foo" class="foo">
Some Text.
<p id="author" class="author">
<br>
<a href="example.com">bar</a>
</p>
</div>
</body>
</html>
脚本.sh
#!/bin/bash
author=$(sed 's/.*<p id="author" class="author"><br><a href="*">\(.*\)<\/a><\/p>.*/\1/p' test.html)
quote=$(sed 's/.*<div id="foo" class="foo">\(.*\)<\/div>.*/\1/p' test.html)
在该行下,我只想要值中的文本。没有html标签。 但是我的脚本不起作用..
【问题讨论】:
-
使用像stackoverflow.com/tags/xmlstarlet/info这样的html解析器而不是正则表达式
-
$author 和 $quote 应该包含什么?
-
我无法使用 xmlstarlet。我没有 sudo 访问权限。
-
@Cyrus author -> "bar" and quote -> "Some Text."