【发布时间】:2014-09-10 10:00:09
【问题描述】:
我需要将 html 文件中的每个“_”替换为“-”,但只能在标签中,并且只能在“name”属性中。
所以每一个:
<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/awk 解析 HTML,避免,避免,避免。
-
看看this answer。
-
@AvinashRaj: 这个 '
' 也匹配 -
@walidtoumi 试试这个
perl -pe 's/(?:<\S+\b[^<>]*? \bname="|(?<!^)\G)[^"_ ]*\K_/-/g' file -
@AvinashRaj:谢谢