【问题标题】:How to grep table HTML file using bash script如何使用 bash 脚本 grep 表格 HTML 文件
【发布时间】:2020-08-14 15:22:52
【问题描述】:

你好兄弟姐妹。

我对我的工作有疑问,但真正的代码是这么多代码,但我只想要这样的 grep 代码 html

<td>USER</td>
 <td><pre class=sf-dump id=sf-dump-957164173 data-indent-pad="  ">"<span class=sf-dump-str title="34 characters">Alex</span>"
 </pre><script>Sfdump("sf-dump-957164173")</script>
    </td>
 </tr>
 <tr>

我只想输出 ALEX

IM 尝试将此命令用于 grep

grep -oP "<td>USER<\/td>\s+<td><pre.*>(.*?)<\/span>" c.html

但是我的命令没有结果,我正在尝试使用命令 sed 但是,我想学习使用 grep。太

谢谢

【问题讨论】:

  • 作业工具错误。见stackoverflow.com/a/1732454/14122
  • ...更好的方法是使用命令行工具,让您对 HTML 运行真正的 XPath 查询(XPath 是一种专门为结构化文档设计的查询语言,也广泛用于 JavaScript特别是与 HTML 交互)。
  • ...所以,有关使用 bash 中的 xpath 的示例,请参阅stackoverflow.com/questions/4984689/…;或(显示如何处理来自输入的不是 XHTML 的 HTML)stackoverflow.com/questions/37072931/…
  • @CharlesDuffy 是的,先生,但我想要因为我想成为 bash 专家 xD,我们不能使用命令 grep sir 进行 grep?
  • @EdoPermata 在你的情况下,我在考虑w3m,它可以用来剥离html标签:w3m c.html -T text/html -dump | grep '^"' | tr -d '"',但xpath确实更适合这种工作。跨度>

标签: bash shell


【解决方案1】:

您可以使用perl(使用适当的开关和正则表达式)提取 c.html 中的数据:

perl -00ne 'print "$1\n" if m{<td>USER</td>\s*<td><pre.+?characters">(.+?)</span>}' c.html

生产

Alex

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 2017-11-09
    相关资源
    最近更新 更多