2014-11-05 73 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完成)。