Insert folder name in text string

Hi,

I am trying to organize a badly constructed web server. Basically every file is in public_html which is horrendous. There's about 50 images and 20 html files (not counting the css files and pdf docs).

I am trying to update each .html file using sed based on:
Where string contains *.jpg or *.gif or *.png
insert 'images/' before the filename.
i.e current HTML is

img src="image.jpg"

, I want to insert 'images/' in front of any jpg filename.

I tried

 sed 's/*.jpg /images/g' *.html 

, but didn't work it just screen printed the file contents without updating anything.

Any help would be appreciated

Thanks

Enginama
:confused:

Hello enginama,

Welcome to forums, thank you for using code tags as per forum rules :b:.
Could you please try following and let me know if this helps you.

for file in *.html
do
    awk -vIMAGE="images\/" '/^img src/{gsub(/[^"]*.jpg|[^"]*.gif|[^"]*.png/,IMAGE"&",$0);} {print}' file > tmp_file
    mv tmp_file file
done

Also this will work in a specific directory, so if you want to run from a remote directory to all another directories then please change *.html to /my/complete/path/*.html .

EDIT: Also there is no need to put \ in vIMAGE="images\/" because while posting it here it is changing it to some other text so it avoid that I have done so in this post.

Thanks,
R. Singh

ok created sh script and placed it in directory with a single html file which had multiple occurences of gif and jpg's.

copied in the below code to the sh file as below:

for file in *.html
do
    awk -vIMAGE="images/" '/^img src/{gsub(/[^"]*.jpg|[^"]*.gif|[^"]*.png/,IMAGE"&",$0);} {print}' file > tmp_file
    mv tmp_file file
done

executed sh by

./changefiles.sh

On first attempt, it couldn't open 'file' for read but it did create an empty file called 'file'
Reran sh and it didn't produce an error, but no output either and 'file' still empty.
Checked permissions and user owns both html, sh (RWX)

Lost on what to do next.

Thanks for your help, it really is appreciated.

Enginama

Hello enginama,

Could you please try following and let me know if this helps, it was having missing $file . Because I had not tested it.

for file in *.html
do
    awk -vIMAGE="images\/" '/^img src/{gsub(/[^"]*.jpg|[^"]*.gif|[^"]*.png/,IMAGE"&",$0);} {print}' $file > tmp_file
    mv tmp_file $file
done

Thanks,
R. Singh

Perhaps a this sed in a for loop written here :

sed 's#\(img src=\)"\(.*\.[jgp][pin][gf]\)"#\1"\2/"#g' "$file" > "$file.tmp" && mv "$file.tmp" "$file"

Above will not match case sensitivity, neither will awk solution, so PNG or Png will not be matched.
Regex would have to match capital letters [Pp] as well or use IGNORECASE if your awk supports it.

RavinderSingh13 your awk only match if a line is beginning with img=src or am i mistaken ?

Hope that helps
Regards
Peasant.

---------- Post updated at 07:18 ---------- Previous update was at 07:14 ----------

Sorry i cannot edit posts, a small correction :

sed 's#\(img src=\)"\(.*\.[jgp][pin][gf]\)"#\1"/my/new/path/\2"#g'

tried updated awk command. same effect, 'file' was still empty, no error messages either.
tried the sed line but getting Bash:permission denied error.
Below is a line of the code in one of the files I was referring to, hoping this helps a little:

<a href="https://www.facebook.com/pages/Charity/67484895"><img src="fb.jpg" alt="facebook" height="75" width="75"/></a>

Thankfully the original author was consistent with using lower case file extensions, so that won't be a concern.

Peasant's second, corrected sed command does what you expect

sed 'p; s#\(img src=\)"\(.*\.[jgp][pin][gf]\)"#\1"./images/\2"#g' file1
<a href="https://www.facebook.com/pages/Charity/67484895"><img src="fb.jpg" alt="facebook" height="75" width="75"/></a>
<a href="https://www.facebook.com/pages/Charity/67484895"><img src="./images/fb.jpg" alt="facebook" height="75" width="75"/></a>

You see the difference before/after; remove the p; in the beginning if happy; use redirection and mv to replace the original file.

RavinderSingh13's awk command needs another small correction to make it fly:

awk -vIMAGE="./images/" '1;/img src/{gsub(/[^"]*.jpg|[^"]*.gif|[^"]*.png/,IMAGE"&");} {print}' file1
<a href="https://www.facebook.com/pages/Charity/67484895"><img src="fb.jpg" alt="facebook" height="75" width="75"/></a>
<a href="https://www.facebook.com/pages/Charity/67484895"><img src="./images/fb.jpg" alt="facebook" height="75" width="75"/></a>

You see the difference before/after; remove the 1; in the beginning if happy; use redirection and mv to replace the original file.

---------- Post updated at 16:29 ---------- Previous update was at 16:25 ----------

This site doesn't allow me to use images/ so I entered ./images/ - edit the directory string to taste.

Awesome. So put into shell script and added for loop.
Seems to work on 2 files I tested it on, so looking promising.
Thanks to everyone for all their help.

#!/bin/bash
for file in *.html;
do
sed 's#\(img src=\)"\(.*\.[jpg][pin][gf]\)"#\1"./images/\2"#g' $file > "$file.tmp" && mv "$file.tmp" "$file"
done

haven't tested it yet with png file types, but it did replace all code lines when it was a gif or jpg, so will do a final test with pngs shortly.

---------- Post updated at 02:49 PM ---------- Previous update was at 02:08 PM ----------

Definitely worked for images, now have to do the same for all the pdf files.
But below code doesn't seem to do what I need, any advice please?

for file in *.html;
do
sed 's#\(a href=\)"\(.*\.[pdf]\)"#\1"./pdfs/\2"#g' $file > "$file.tmp" && mv "$file.tmp" "$file"
done

If you are looking for href strings containing .pdf (lowercase only), you need to change \.[pdf] to .pdf . The expression you have is looking for strings containing .p , ,d or .f . If you wanted a case independent match, you would want something more like \.[Pp][Dd][Ff] (which would match strings containing .PDF , .PDf , .PdF , .Pdf , .pDF , .pDf , .pdF , and .pdf ).

Thanks that fixed it.

I really appreciate everyone's help. :smiley: