[英]Replace character only in certain string
I need to replace every "_" with "-" in html file but only in tag and only in "name" attribute. 我需要在html文件中用“-”替换每个“ _”,但只能在标记中,并且仅在“名称”属性中。
So every this: 所以每一个:
<a name="menu_portlet_test"> or <a name="whatever_is_here">
should become this: 应该变成这个:
<a name="menu-portlet-test"> and <a name="whatever-is-here">
Can't figure out how to force something like sed/awk to do it. 无法弄清楚如何强制执行sed / awk之类的操作。 Help!
救命!
sed ':a
s/\(<[^>]* name="[^"]*\)_\([^"]*"\)/\1-\2/g;ta' YourFile
Should do most of you job. 应该做你们大多数人的工作。 Not perfect due top html possibilities but should be 99,9% ok
由于顶部html的可能性不理想,但应该99.9%可以
explaination 解释
s//g
<
followed by any non >
([^>] ) followed by
name=" followed by (any non
" (
[^"] ) ) [ as group 1] followed by
[so first
between quote after name=] followed by ( any non
" (
[^"]* ) followed by
"`) [ as group 2 ] <
后跟任何非>
([^>] ) 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] -
followed by content of group 2 -
然后是组2的内容 g
do it for any occurence on the line. g
进行任何在线处理。 This change 1 _ per name="" but on any name= of the line. <... name="bla_bla_bla"> ... <... name="other_bla_bla"> ...
change to <... name="bla-bla_bla"> ... <... name="other-bla_bla"> ...
<... name="bla_bla_bla"> ... <... name="other_bla_bla"> ...
更改为<... name="bla-bla_bla"> ... <... name="other-bla_bla"> ...
ta
s//
, redo the same action with modified content( in fact it is a if/goto to label :a
) s//
发生了更改,请对修改后的内容重做相同的操作(实际上,它是对if:go的标签:a
) Use a proper HTML handling tool, for example xsh , a wrapper around Perl's XML::LibXML . 使用适当的HTML处理工具,例如xsh ,它是Perl的XML :: LibXML的包装器。 The following commands can be saved in a script, or entered from its interactive environment:
以下命令可以保存在脚本中,也可以从其交互式环境中输入:
open :F html file.html ;
for //@name set . xsh:subst(., '_', '-', 'g') ;
save :b ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.