'SED - Prefix all jpg files with full URL
I have an HTML file with local paths to images like so - /image1.jpg or /image2.png
I want to use SED to prefix a desired url to that like so - https://example.com.
Desired result is https://example.com/image1.jpg
I tried this -
sed "s#.*.jpg#https://example.com/*.jpg#g" index.html > index2.html
The result is index2.html which finds the image names but my replacement does not use those names. How to keep that match name?
Any suggestions?
Sample Input -
<img src="Image1.jpg" alt="boat" /><figcaption>boat</figcaption>
Sample Output -
<img src="https://example.com/image1.jpg" alt="boat" /><figcaption>boat</figcaption>
Solution 1:[1]
You can use
sed 's~\(="\)\([^"]*\.jpg"\)~\1https://example.com/\2~g'
Details:
\(="\)- Group 1 (\1):="string\([^"]*\.jpg"\)- Group 2 (\2): any zero or more chars other than"and then.jpg"substring.
See the online demo:
#!/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>
Solution 2:[2]
This is what finally worked easily. Thanks for everyone's input. Trying to get it to work in a batch file on Windows was painful.
Command line -
sed -f sedscript.sed index.html > index2.html
Content of sedscript.sed
s#src="#&https://example.com/#g
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wiktor Stribiżew |
| Solution 2 | tyee |
