简体   繁体   中英

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>

Using sed

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

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM