[英]Replace character only in certain string
我需要在html文件中用“-”替換每個“ _”,但只能在標記中,並且僅在“名稱”屬性中。
所以每一個:
<a name="menu_portlet_test"> or <a name="whatever_is_here">
應該變成這個:
<a name="menu-portlet-test"> and <a name="whatever-is-here">
無法弄清楚如何強制執行sed / awk之類的操作。 救命!
sed ':a
s/\(<[^>]* name="[^"]*\)_\([^"]*"\)/\1-\2/g;ta' YourFile
應該做你們大多數人的工作。 由於頂部html的可能性不理想,但應該99.9%可以
解釋
s//g
<
后跟任何非>
([^>] ) followed by
name =“, followed by (any non
” (
[^“] ) ) [ as group 1] followed by
[so first
between quote after name=] followed by ( any non
[so first
between quote after name=] followed by ( any non
“ (
[^”] * ) followed by
“`)[作為組2] -
然后是組2的內容 g
進行任何在線處理。 每行name =“”更改1 _,但在該行的任何name =上。 <... name="bla_bla_bla"> ... <... name="other_bla_bla"> ...
更改為<... name="bla-bla_bla"> ... <... name="other-bla_bla"> ...
ta
s//
發生了更改,請對修改后的內容重做相同的操作(實際上,它是對if:go的標簽:a
) 使用適當的HTML處理工具,例如xsh ,它是Perl的XML :: LibXML的包裝器。 以下命令可以保存在腳本中,也可以從其交互式環境中輸入:
open :F html file.html ;
for //@name set . xsh:subst(., '_', '-', 'g') ;
save :b ;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.