replace文本文件中的模式

在我的示例html文件下面:

some text here <img src="http://site.com/7b399e20/77165/5fa/2a31ffb8.jpg"/> sometext here some text here <img src="http://site.com/7b399e20/2a31ffb8.jpg"/> sometext here some text here <img src="http://site.com/7b399e20/2a31ffb8.png"/> sometext here some text here <img src="http://site.com/2a31ffb8.jpeg"/> sometext here 

我如何进行这样的转换:

 some text here <img src="web/2a31ffb8.jpg"/> sometext here some text here <img src="web/2a31ffb8.jpg"/> sometext here some text here <img src="web/2a31ffb8.png"/> sometext here some text here <img src="web/2a31ffb8.jpeg"/> sometext here 

谢谢

我将使用Perl,因为我知道语法而不必查看它,但是在awk或sed中会非常相似,正如tekknolagi所说:

 perl -pi -e 's|http://site.com/.*([^/]+)"/>|web/$1"/>|;' <filename> 

这将保存最后/和“

 sed -i 's:\(img src="\).*\(/[^"/]\+\.[^"]\+"\):\1web\2:' INPUTFILE 

可以做到位。

HTH

如何使用perl脚本? 我已经把你的示例文本文件foo.txt ,这里是结果:

 $ cat foo.txt | perl -pe 's#http://.*/([a-z0-9A-Z]*\.)#web/\1#' some text here <img src="web/2a31ffb8.jpg"/> sometext here some text here <img src="web/2a31ffb8.jpg"/> sometext here some text here <img src="web/2a31ffb8.png"/> sometext here some text here <img src="web/2a31ffb8.jpeg"/> sometext here 

使用sedawk你可以做到这一点