
我需要刪除<li>
許多index.html 頁面中的以下html 標記。
<li>
<a href="https://forward.global.ssl.fastly.net/contributoragreements/" onclick="_gaq.push(['_trackEvent', 'ClickTracking', 'TopNav_Contact_Editorial', window.location.href]);">Editorial</a>
</li>
我需要在許多文件中遞歸地刪除它。所以我想在linux的sed中使用正規表示式是最好的選擇。我嘗試了很多方法但找不到解決方案。 index.html 檔案中還有其他<li>
標籤,但無論如何都不應該編輯它們。僅應刪除上述標籤。
提前謝謝了。
答案1
假設文件片段是格式良好的 XHTML 檔案的一部分,您可以刪除包含其屬性值恰好為using 的節點li
的所有節點:a
href
https://forward.global.ssl.fastly.net/contributoragreements/
xmlstarlet
xmlstarlet ed --delete '//li[a/@href = "https://forward.global.ssl.fastly.net/contributoragreements/"]' file.xhtml
如果文檔不是格式良好的 XHTML 文檔,您可以先嘗試恢復它:
xmlstarlet fo --recover --html file.html |
xmlstarlet ed --delete '//li[a/@href = "https://forward.global.ssl.fastly.net/contributoragreements/"]'
index.html
若要對rotted 目錄結構中的所有檔案執行此命令top-dir
,請xmlstarlet
像find
這樣呼叫:
find top-dir -type f -name index.html -exec sh -c '
tmpfile=$(mktemp)
for pathname do
cp "$pathname" "$tmpfile"
xmlstarlet fo --recover --html "$tmpfile" |
xmlstarlet ed --delete "//li[a/@href = \"https://forward.global.ssl.fastly.net/contributoragreements/\"]" >"$pathname.new"
done
rm -f "$tmpfile"' sh {} +
上面的程式碼將為index.html.new
每個找到的index.html
檔案建立一個新檔案。在.new
從上面的命令中刪除運行之前,您應該查看這些檔案並確定它們看起來是否正常。
顯然你應該在複製測試時備份的資料。