我需要从位于不同子目录中的一组html文件中获取所有内联的“数据标题”属性值。有什么简单的方法可以在linux机器上做到这一点吗?
我在另一个这样的帖子上发现了类似的东西,试图编辑它,但我是一个新手:
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>发布于 2014-11-05 17:15:29
是的,使用xmllint (regex不是解析xmllint的合适工具):
$ find . -iname '*.html' -exec xmllint --html --xpath '//node/title' {} \;或者使用bash4:
$ xmllint --html --xpath '//node/title' **/*.html其中节点是节点的名称,包括title元素。
编辑
xmllint或xmlstarlet也不能正确地解析这个HTML。一个快速工作的黑客是使用:
grep -oP 'data-title="\K[^"]+' *files发布于 2014-11-05 17:19:23
或者,您可以使用(e)grep。
grep -e'<title>.*<\/title>' *.html
egrep "<title>.*?<\/title>" *.html
从文件夹里。
使用
grep -re'<title>.*<\/title>' */*.html
解析子目录和
grep -rhe'<title>.*<\/title>' */*.html
要解析子目录并省略文件名显示,如果您只想要标题行的话。
发布于 2014-11-05 17:39:53
如果需要,可以使用sed并取出title标记数据,如果需要从元链接数据中获取它,则必须更改它:
sed -n 's#.*<title>\(.*\)</title>.*#\1#p' *.html如果它们在同一条线路上,则应该这样做:
sed -n "/title=/s/.* title=\"\(.*\)\".*/\1/p"否则,您需要将其修改为多行匹配(仍然可以使用sed完成)。
https://stackoverflow.com/questions/26763073
复制相似问题