I need to replace every "_" with "-" in html file but only in tag and only in "name" attribute.
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. 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
explaination
s//g
- Search the pattern (
<
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 ]
- Replace it by content of group 1 followed by
-
followed by content of group 2
g
do it for any occurence on the line. 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"> ...
ta
- if a change occur in previous
s//
, redo the same action with modified content( in fact it is a if/goto to label :a
)
Use a proper HTML handling tool, for example xsh, a wrapper around Perl's 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 ;