2013-09-01 52 views
0

我有这样sed的 - 文本添加变量

<a href="/site/index.php/Something" title="Something">Something cool</a>, <a href="/site/index.php/Nice_Text" title="Nice Text">Nice Text</a> 
some text 
<a href="/site/index.php/Apple%27s_text" title="Apple's text">Apple's text</a> 

HTML代码,我需要添加点(开始)和.html的(端)的链接得到这个:

<a href="./site/index.php/Something.html" title="Something">Something cool</a>, <a href="./site/index.php/Nice_Text.html" title="Nice Text">Nice Text</a> 
some text 
<a href="./site/index.php/Apple%27s_text.html" title="Apple's text">Apple's text</a> 

我正在玩sed,但我不知道,如何与更改的网址一起工作。 类似于 查找"/site/index.php/并首次出现"之前".html(或之间的变量之后)。

谢谢。

回答

1
sed 's/<a \+href="\([^\"]*\)"/<a href=".\1.html"/g' my_file.html 

这看起来对任何看起来像<a href="xxx"和替换xxx.xxx.html。它允许在ahref之间有多个空格。要查找xxx,它会查找不包含""之间的任何字符串。假设您的原始文件包含前面的/,如您的示例所示,并且<a href="xxx"全部位于文件的同一行(例如,在ahref之间未中断)。 g选项将确保它在一条线上处理多个href

+0

你的假设是正确的。谢谢。 – noideahere

0

用awk

awk '{gsub(/href="/,"&.");gsub(/" title/,".html&")}1' file