Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
150 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 firstbetween 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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...