Issue
Let's say I have the file: index.html
index.html
<div class="familylinks">
<div class="parentlink"><strong>Parent_topic:</strong> <a class="link" href="../some_text_here_a_b" title="Sample html Page here">Sample_html_page</a></div>
</div>
</div><div class="runningfooter" 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 class="familylinks">
<div class="parentlink"><strong>Parent_topic:</strong> <a class="link" href="../some-text-here-a-b" title="Sample html Page here">Sample_html_page</a></div>
</div>
</div><div class="runningfooter" 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
Solution
Using sed
$ sed -E ':a;s/(href="[^"]*)_/\1-/;ta' input_file
<div class="familylinks">
<div class="parentlink"><strong>Parent_topic:</strong> <a class="link" href="../some-text-here-a-b" title="Sample html Page here">Sample_html_page</a></div>
</div>
</div><div class="runningfooter" align="center">
Answered By - sseLtaH Answer Checked By - Terry (WPSolving Volunteer)