Home > Net >  sed: replace _ within quotes with -
sed: replace _ within quotes with -

Time:12-20

Let's say I have the file: index.html

index.html

<div >
<div ><strong>Parent_topic:</strong> <a  href="../some_text_here_a_b" title="Sample html Page here">Sample_html_page</a></div>
</div>
</div><div  align="center">

How can i replace _ to - within href="../some_text_here_a_b" (double quotes of href)

For example, changing index.html to:

<div >
<div ><strong>Parent_topic:</strong> <a  href="../some-text-here-a-b" title="Sample html Page here">Sample_html_page</a></div>
</div>
</div><div  align="center">

Tried with,

sed -e "/href/s/_/-/g" index.html

but it is replacing all _ to - present on line which having href word on line

CodePudding user response:

Using sed

$ sed -E ':a;s/(href="[^"]*)_/\1-/;ta' input_file
<div >
<div ><strong>Parent_topic:</strong> <a  href="../some-text-here-a-b" title="Sample html Page here">Sample_html_page</a></div>
</div>
</div><div  align="center">
  • Related