[英]Command Line search all html files, retrieve attribute value
我需要从位于不同子目录中的一堆html文件中获取所有内联的 “数据标题”属性值。 在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>
是的,使用xmllint
(正则表达式不是解析HTML的正确工具):
$ find . -iname '*.html' -exec xmllint --html --xpath '//node/title' {} \;
或使用bash4 :
$ xmllint --html --xpath '//node/title' **/*.html
其中node是包括title元素的节点的名称。
编辑
xmllint
或xmlstarlet
都无法正确解析此HTML。 一个快速工作的技巧是使用:
grep -oP 'data-title="\K[^"]+' *files
或者,您可以使用(e)grep
grep -e'<title>.*<\\/title>' *.html
egrep "<title>.*?<\\/title>" *.html
从文件夹中。
采用
grep -re'<title>.*<\\/title>' */*.html
解析子目录并
grep -rhe'<title>.*<\\/title>' */*.html
如果只需要标题行,则解析子目录并忽略文件名显示。
您可以使用sed并根据需要提取标题标签数据,如果需要从某些元链接数据中获取数据,则必须进行更改:
sed -n 's#.*<title>\(.*\)</title>.*#\1#p' *.html
如果它们在同一行上,则应该这样做:
sed -n "/title=/s/.* title=\"\(.*\)\".*/\1/p"
否则,您需要将其修改为多行匹配(仍然可以使用sed完成)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.