UNIX 解析 HTML 页面显示标签内容 - One Liner?

发布于 2024-09-03 06:37:08 字数 380 浏览 2 评论 0 原文

我有一个 HTML 文件,我对

 所包含的数据感兴趣。 
标签。有没有一种单线可以实现这一目标?

示例文件:

<html>
<title>
Hello There!
</title>
<body>
<pre>
John   Working
Kathy  Working
Mary   Working
Kim    N/A
</pre>
</body>
</html>

输出应该是:

John 
Kathy 
Mary 
Kim 

非常感谢大家,谢谢!

I have an HTML file and I am interested in the data enclosed by <pre> </pre> tags. Is there a one-liner that can do achieve this?

Sample file :

<html>
<title>
Hello There!
</title>
<body>
<pre>
John   Working
Kathy  Working
Mary   Working
Kim    N/A
</pre>
</body>
</html>

Output should be :

John 
Kathy 
Mary 
Kim 

Much appreciated guys, thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一页 2024-09-10 06:37:08

亲自使用twig 工具。它拥有的功能之一是名为 xml_grep 的东西。您的问题简化为

cat foo.txt | xml_grep --nowrap pre 

pre is an xpath 表达式。
接下来是一些简单的文本处理,即使您的 XML 格式不同,这也会起作用。

建议 - 不要使用 sed 和其他基于流的文本处理工具来操作 XML 等结构化数据。使用适当的解析器。

Get your hands on the twig tools. One of the things it has is something called xml_grep. Your problem reduces into

cat foo.txt | xml_grep --nowrap pre 

pre is an xpath expression.
Followed by some simple text processing and this will work even if your XML is formatted differently.

Word of advice - don't use sed and other stream based text processing tools to manipulate structured data like XML. Use a proper parser.

一抹淡然 2024-09-10 06:37:08

如果您有 XHTML,则使用 xmlstarlet。如果不这样做,那么首先将其传递给 HTML Tidy,然后将其转换为 XHTML。

If you have XHTML then use xmlstarlet. If you don't then pass it through HTML Tidy first, and turn it into XHTML.

葵雨 2024-09-10 06:37:08

由于您专门询问了使用 sed 的解决方案...假设有趣的行始终位于包含

的行之间(看起来完全像)并且有趣的内容永远不会与开始或结束标记在同一行,并假设第一个这样的块是您想要提取的唯一块,并假设虽然您了解这确实是 错误的方法来解决你仍然想要的这个问题要做到这一点,那么您可以使用 sed 来执行此操作,例如如下所示:

sed '1,/<pre>/d;/<\/pre>/,$d'

它删除从第一行到包含

 的所有行以及包含 < 的行中的所有行;/pre> 到最后。

(FWIW,我宁愿使用 XPath 表达式来选择感兴趣的内容。例如使用 xmlstarlet 按照 Ignacio Vazquez-Abrams 的建议,它可以像这样:xmlstarlet sel -t -v /html/body/pre .)

Since you specifically asked about a solution using sed... Assuming that the interesting lines are always between lines containing <pre> and </pre> (appearing exactly like that) and that the interesting content is never on the same line than the opening or closing tag, and assuming that the first such block is the only one you want to extract, and assuming that while you understand that this is really the wrong way to solve this problem you still want to do it, then you could do this using sed for example like this:

sed '1,/<pre>/d;/<\/pre>/,$d'

It deletes all lines from the first up to the one containing <pre> and all lines from the one containing </pre> to the last.

(FWIW, I would rather use an XPath expression for selecting the interesting content. For example using xmlstarlet as suggested by Ignacio Vazquez-Abrams it could go like this: xmlstarlet sel -t -v /html/body/pre.)

腹黑女流氓 2024-09-10 06:37:08

我的 Perl-fu 很弱,但这适用于您的示例:

$ cat file.html | perl -e'while(<>){if(/<\/pre>/){$a=0;}if($a){print}if(/<pre>/){$a=1;}}' | cut -f1 -d' '
John
Kathy
Mary
Kim

My Perl-fu is weak, but this works for your example:

$ cat file.html | perl -e'while(<>){if(/<\/pre>/){$a=0;}if($a){print}if(/<pre>/){$a=1;}}' | cut -f1 -d' '
John
Kathy
Mary
Kim
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文