【发布时间】:2014-07-28 17:25:53
【问题描述】:
我在一些需要修改的文件中有一些 html 链接:
<a href="www.blah.edu/hello world of friends" class="blue">Hello World</a>
假设上述链接位于 test.txt 中。
我一直在尝试找到一种单行符,它可以在 href 链接中用下划线仅替换空格。所以
<a href="www.blah.edu/hello world of friends" class="blue">Hello World</a>
应该是
<a href="www.blah.edu/hello_world_of_friends" class="blue">Hello World</a>
据我所知,使用 sed:
sed '/href=['"'"'"][^"'"'"']*['"'"'"]*"/{s;\s;_;g}' test.txt
当然,这会产生:
<a_href="www.blah.edu/hello_world"_class="blue">Hello_World</a>
我明白为什么会这样。 /regex/ 位将整行拉入模式空间,然后 s;;;在整行上执行,而不仅仅是我需要的位。
如何仅在 href= 和 " 内用空格替换下划线?有没有比使用 sed 更好的方法来考虑这个问题?
【问题讨论】: