在我的示例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
由于
答案 0 :(得分:3)
我将使用Perl,因为我知道语法而不必查找它,但它在awk或sed中非常相似,正如tekknolagi所说:
perl -pi -e 's|http://site.com/.*([^/]+)"/>|web/$1"/>|;' <filename>
这将保留最后一个/和“
之间的所有内容答案 1 :(得分:1)
sed -i 's:\(img src="\).*\(/[^"/]\+\.[^"]\+"\):\1web\2:' INPUTFILE
可能会到位。
HTH
答案 2 :(得分:-1)
使用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
答案 3 :(得分:-3)
使用sed
或awk
您可以执行此操作