0

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!

5
  • 3
    HTML parsing using sed/awk, avoid, avoid, avoid. Commented Sep 10, 2014 at 10:02
  • Have a look at this answer. Commented Sep 10, 2014 at 10:08
  • @AvinashRaj: this '<dir badname="whatever_is_here">' also match Commented Sep 10, 2014 at 10:53
  • 1
    @walidtoumi try this perl -pe 's/(?:<\S+\b[^<>]*? \bname="|(?<!^)\G)[^"_ ]*\K_/-/g' file Commented Sep 10, 2014 at 10:58
  • where? regex101.com/r/dK5dX1/2 Commented Sep 10, 2014 at 11:03

2 Answers 2

2
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)
Sign up to request clarification or add additional context in comments.

2 Comments

seems to do the trick, thanks, now i'll have to figure out how it works :). EDIT: Aaah! a loop, brilliant!
added some comment to explain
1

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 ;

1 Comment

Looks interesting, so are these commands that you run from within an interactive shell? Perhaps some more detail in the answer would be useful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.