[英]SED - Prefix all jpg files with full URL
我有一个 HTML 文件,其中包含图像的本地路径 - /image1.jpg 或 /image2.png
我想使用 SED 来为所需的 url 加上前缀 - https://example.com 。
期望的结果是https://example.com/image1.jpg
我试过这个 -
sed "s#.*.jpg#https://example.com/*.jpg#g" index.html > index2.html
结果是 index2.html,它找到了图像名称,但我的替换不使用这些名称。 如何保留匹配名称?
有什么建议么?
样品输入 -
<img src="Image1.jpg" alt="boat" /><figcaption>boat</figcaption>
样品 Output -
<img src="https://example.com/image1.jpg" alt="boat" /><figcaption>boat</figcaption>
使用sed
$ sed 's#="#&https://example.com#' input_file
<p><img src="https://example.com/image1.jpg" /></p>
您可以使用
sed 's~\(="\)\([^"]*\.jpg"\)~\1https://example.com/\2~g'
详情:
\(="\)
- 第 1 组 ( \1
): ="
字符串\([^"]*\.jpg"\)
- 第 2 组 ( \2
):除"
然后.jpg"
substring 之外的任何零个或多个字符。请参阅在线演示:
#!/bin/bash
s='<img src="Image1.jpg" alt="boat" /><figcaption>boat</figcaption>'
sed 's~\(="\)\([^"]*\.jpg"\)~\1https://example.com/\2~g' <<< "$s"
Output:
<img src="https://example.com/Image1.jpg" alt="boat" /><figcaption>boat</figcaption>
这就是最终轻松完成的工作。 感谢大家的投入。 试图让它在 Windows 上的批处理文件中工作是痛苦的。
命令行 -
sed -f sedscript.sed index.html > index2.html
sedscript.sed 的内容
s#src="#&https://example.com/#g
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.