2012-09-03 89 views
0

什麼是正則表達式在這個標籤中查找文本「這是標題」?使用Grep,Sed或Awk。使用Bash查找H1文本

代碼示例:

<h1 class="round title"> 
    <a href="/somepage">This is the title</a> 
</h1> 

我已經試過這上面h1標籤。

curl --silent http://domain.com/index.html | grep "<h1 class=\"round title\">" 

結果是:

<h1 class="round title"><a href="/somepage">This is the title</a></h1> 

,我只需要 「這是標題」 的一部分。

+1

對於一般情況,您需要使用HTML解析器。 – pizza

+0

如果我需要使用HTML解析器,我會的。但我需要bash =) – Tux

+0

'grep'在這裏完全沒有問題,因爲它一次只能在一條線上運行。 Sed或awk可以處理簡單的情況,但爲了充分處理結構化數據,您確實需要使用可以處理結構的工具。見例如http://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-not – tripleee

回答

1

我用下面的命令得到它。

curl --silent http://domain.com/index.html | grep -E "<h1.*><a.*>(.*?)</a></h1>" | sed 's/.*<a.*>\(.*\)<\/a>.*/\1/' 

謝謝大家。