我從未使用過 grep 來解析 HTML 文件,並且遇到以下問題:
grep -Po "(?s)(<h2>.+?<!-- /endcontent -->)" input.html > output.html
-bash: !--: event not found
我也嘗試過…
grep -Po "(?s)(<h2>.+?\<!-- \/endcontent --\>)" input.html > output.html
……無濟於事。
有沒有辦法讓 grep/bash 解析 HTML 文件,或者我應該使用其他應用程式?
謝謝。
編輯:看起來 shell 和 grep 之間有一個技巧。搜尋確切的字串「test」有效,而「t.st」和「t.st」都無效。有人知道為什麼嗎?
編輯:出於某種原因,“-P”選項破壞了正規表示式。
編輯:是的,“-P”選項需要將代表任何字元的點字元加倍。詭異的。
grep -Po 't..st' input > output
答案1
嘗試使用單引號'
代替,如下所示:
grep -Po '(?s)(<h2>.+?<!-- /endcontent -->)' input.html > output.html
當事物被雙引號引用時,Grep 的行為會有所不同。