【问题标题】:Command Line search all html files, retrieve attribute value命令行搜索所有 html 文件,检索属性值
【发布时间】:2015-01-01 23:48:00
【问题描述】:

我需要从位于不同子目录中的一堆 html 文件中获取所有 inline "data-title" 属性值。有没有一种简单的方法可以在 linux 机器上做到这一点?

我在另一个 SO 帖子中发现了类似的内容,并尝试对其进行编辑,但我是 sed 新手:

sed "s/.* data-title=\"\(.*\)\".*/\1/"

我无法完全正确地完成这部分,我认为我需要使用一个额外的搜索实用程序才能使其正常工作。理想情况下,我希望将所有输​​出都保存到 txt 文件中。

样本:

    <aside class="grid-sidebar sidebar">
        <div id="listLoading"><div id="loading-listLoading" class="front-center" style="padding-top: 22%; top: 0%; display: none;"><div style="width: 42px; height: 42px; position: absolute; margin-top: 17px; margin-left: -21px; -webkit-animation: spin12 0.8s linear infinite;"><svg style="width: 42px; height: 42px;"><g transform="translate(21,21)"><g stroke-width="4" stroke-linecap="round" stroke="rgb(34, 34, 34)"><line x1="0" y1="11" x2="0" y2="18" transform="rotate(0, 0, 0)" opacity="1"></line><line x1="0" y1="11" x2="0" y2="18" transform="rotate(30, 0, 0)" opacity="0.9173553719008265"></line><line x1="0" y1="11" x2="0" y2="18" transform="rotate(60, 0, 0)" opacity="0.8347107438016529"></line><line x1="0" y1="11" x2="0" y2="18" transform="rotate(90, 0, 0)" opacity="0.7520661157024794"></line><line x1="0" y1="11" x2="0" y2="18" transform="rotate(120, 0, 0)" opacity="0.6694214876033058"></line><line x1="0" y1="11" x2="0" y2="18" transform="rotate(150, 0, 0)" opacity="0.5867768595041323"></line><line x1="0" y1="11" x2="0" y2="18" transform="rotate(180, 0, 0)" opacity="0.5041322314049588"></line><line x1="0" y1="11" x2="0" y2="18" transform="rotate(210, 0, 0)" opacity="0.42148760330578516"></line><line x1="0" y1="11" x2="0" y2="18" transform="rotate(240, 0, 0)" opacity="0.33884297520661155"></line><line x1="0" y1="11" x2="0" y2="18" transform="rotate(270, 0, 0)" opacity="0.25619834710743805"></line><line x1="0" y1="11" x2="0" y2="18" transform="rotate(300, 0, 0)" opacity="0.17355371900826455"></line><line x1="0" y1="11" x2="0" y2="18" transform="rotate(330, 0, 0)" opacity="0.09090909090909094"></line></g></g></svg></div></div></div>
        <div id="list" style="position:relative;">
<div style="height: 55px;">
    <h2 class="heading" style="margin-bottom: 10px">Available Records</h2>
</div>
<div style="height: 51px">
            <div class="grid-3-4">
            <label for="searchInput" class="infield" style="position: absolute; left: 0px; top: 55px; display: block;">Search</label>
            <input id="searchInput" type="text" name="searchInput" data-title="title1" title="" style="height: 36px" class="input-long">
    </div>
    <div class="grid-1-4">
    <select id="listStatus" name="status" class="styled input-full hasCustomSelect" data-title="Title 2" title="" style="-webkit-appearance: menulist-button; width: 104px; position: absolute; opacity: 0; height: 36px; font-size: 16px;">
        <option value="all">All</option>
        <option value="active" selected="">Active</option>
        <option value="archived">Archived</option>
    </select><span class="customSelect styled input-full" style="display: inline-block;"><span class="customSelectInner" style="width: 100%; display: inline-block;">Active</span></span>
    </div>
</div>
    </aside>

【问题讨论】:

    标签: linux command-line sed find


    【解决方案1】:

    是的,xmllint(正则表达式不是解析 HTML 的正确工具):

     $ find . -iname '*.html' -exec xmllint --html --xpath '//node/title' {} \;
    

    或者

     $ xmllint --html --xpath '//node/title' **/*.html
    

    其中 node 是节点的名称,包括标题元素。

    编辑

    xmllintxmlstarlet 也无法正确解析此 HTML。一个快速工作的技巧是使用:

    grep -oP 'data-title="\K[^"]+' *files
    

    【讨论】:

    • 我知道问题是关于 LINUX。只想添加两个 xmllint 命令在 OSX 上都失败。
    • 失败是什么意思?可以安装xmllint,见stackoverflow.com/questions/20391717/…
    • 我在尝试运行这些时得到“未知选项--xpath”,如果所有节点名称都不同怎么办?
    • 您应该升级xmllint 或改用xmlstarlet
    • 好的,我已经升级了 xmllint,现在我得到标签错误,我想我应该提到它是 html5?
    【解决方案2】:

    您也可以使用 (e)grep

    grep -e'&lt;title&gt;.*&lt;\/title&gt;' *.html

    egrep "&lt;title&gt;.*?&lt;\/title&gt;" *.html

    从文件夹中。

    使用

    grep -re'&lt;title&gt;.*&lt;\/title&gt;' */*.html

    解析子目录和

    grep -rhe'&lt;title&gt;.*&lt;\/title&gt;' */*.html

    解析子目录并省略文件名显示,如果你只想要标题行。

    【讨论】:

    • OP 说他有很多子目录,而正则表达式不是用来解析 HTML
    【解决方案3】:

    如果需要,您可以使用 sed 并提取标题标签数据,如果您需要从某些元链接数据中获取它,则必须更改它:

    sed -n 's#.*<title>\(.*\)</title>.*#\1#p' *.html
    

    如果它们在同一行,应该这样做:

    sed -n "/title=/s/.* title=\"\(.*\)\".*/\1/p"
    

    否则需要修改为多行匹配(还是可以用sed完成的)。

    【讨论】:

      猜你喜欢
      • 2018-11-25
      • 2022-07-19
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多