2014-11-05 75 views
0

我需要从位于不同子目录中的一堆html文件中获取所有内嵌“data-title”属性值。有没有一种简单的方法在Linux机器上执行此操作?命令行搜索所有html文件,检索属性值

我找到了另一个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> 

回答

1

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

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

或者与

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

其中节点是包含标题元素的节点的名称。

编辑

xmllintxmlstarlet可以正常解析这个HTML。快速工作黑客是使用:

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

我知道问题是关于LINUX的。只想添加两个xmllint命令在OSX上失败。 – ABri 2014-11-05 17:47:17

+0

你的失败是什么意思?您可以安装'xmllint',请参阅http:// stackoverflow。com/questions/20391717 /我怎么安装最新版本的xmllint-on-osx-10-7-5 – 2014-11-05 17:50:31

+0

当我尝试运行这些时,我得到“未知选项--xpath” ,如果所有的节点名称都不一样? – webaholik 2014-11-05 20:39:08

0

或者您可以使用(e)从文件夹中的grep

grep -e'<title>.*<\/title>' *.html

egrep "<title>.*?<\/title>" *.html

使用

grep -re'<title>.*<\/title>' */*.html

解析子目录和

grep -rhe'<title>.*<\/title>' */*.html

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

+0

OP说他有很多子目录,而且正则表达式不是为了解析HTML – 2014-11-05 17:21:27

0

您可以使用SED和拉标题标签数据,如果你想,如果你需要从一些元链接数据得到它,那么你就必须改变它:

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

如果他们是在同一行本应该做的:

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

否则,你需要对其进行修改,以多赛(它仍然可以使用sed完成)。