Home > Back-end >  SED - Prefix all jpg files with full URL
SED - Prefix all jpg files with full URL

Time:03-04

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>

CodePudding user response:

Using sed

$ sed 's|=\"|&https://example.com|' input_file
<p><img src="https://example.com/image1.jpg" /></p>

CodePudding user response:

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>
  • Related