Insert folder name in text string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Insert folder name in text string
# 1  
Old 02-20-2016
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
Code:
img src="image.jpg"

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

I tried
Code:
 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
Smilie
# 2  
Old 02-20-2016
Hello enginama,

Welcome to forums, thank you for using code tags as per forum rules Smilie.
Could you please try following and let me know if this helps you.
Code:
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

Last edited by RavinderSingh13; 02-20-2016 at 11:04 PM.. Reason: Added a comment for user
# 3  
Old 02-20-2016
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:
Code:
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
Code:
./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
# 4  
Old 02-21-2016
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.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 02-21-2016
Perhaps a this sed in a for loop written here :
Code:
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 :
Code:
sed 's#\(img src=\)"\(.*\.[jgp][pin][gf]\)"#\1"/my/new/path/\2"#g'

This User Gave Thanks to Peasant For This Post:
# 6  
Old 02-21-2016
tried updated awk command. same effect, 'file' was still empty, no error messages either.
tried the sed line but getting BashSmilieermission denied error.
Below is a line of the code in one of the files I was referring to, hoping this helps a little:
Code:
<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.
# 7  
Old 02-21-2016
Peasant's second, corrected sed command does what you expect
Code:
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:
Code:
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.
This User Gave Thanks to RudiC For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert text after the first occurance of searched string entry in a file

My server xml file has huge data part of which i'm sharing below. I wish to add the below text held by variable "addthisline" after the closing braces i.e --> once the first </Connector> tag is found. addthisline="I need to be inserted after the comments" Thus my searchstring is... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Insert a user input string after matched string in file

i am having file like this #!/bin/bash read -p 'Username: ' uservar match='<color="red" />' text='this is only a test so please be patient <color="red" />' echo "$text" | sed "s/$match/&$uservar\g" so desireble output what i want is if user type MARIA this is only a test so please... (13 Replies)
Discussion started by: tomislav91
13 Replies

3. Shell Programming and Scripting

Insert String every n lines, resetting line counter at desired string

I need to read a text file and insert a string every n lines, but also have the line counter restart when I come across a header string. Line repeating working every 3 lines using code: sed '0~3 s/$/\nINSERT/g' < INPUT/PATH/FILE_NAME.txt > OUTPUT/PATH/FILE_NAME.txt I cannot seem to find... (1 Reply)
Discussion started by: Skonectthedots
1 Replies

4. UNIX for Dummies Questions & Answers

Insert Text on lines having the string word

I need help on how I can accomplish my task. I hope someone can help me since I've researching and trying to accomplish this for hours now. Basically, I need to comment-out (or insert a # sign in the beginning of the line) a line when the line has the specific word I am searching. Example I have... (3 Replies)
Discussion started by: Orbix
3 Replies

5. Shell Programming and Scripting

How to insert string at particular 4th line in text file

I just want to add a particular string in text file using shell script text file format 1 columns-10 2 text=89 3 no<> 4 5 test-9876 6 size=9 string need to insert in 4th line <switch IP='158.195.2.567' port='5900' user='testc' password='asdfrp' Code='8'> After inserting the... (8 Replies)
Discussion started by: puneet.goel
8 Replies

6. Shell Programming and Scripting

Find all text files in folder and then copy to a new folder

Hi all, *I use Uwin and Cygwin emulator. I´m trying to search for all text files in the current folder (C/Files) and its sub folders using find -depth -name "*.txt" The above command worked for me, but now I would like to copy all found text files to a new folder (C/Files/Text) with ... (4 Replies)
Discussion started by: cgkmal
4 Replies

7. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

8. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

9. Shell Programming and Scripting

String in text matches folder name

Hi, I need unix shell script that can read the first column of a text file and matching each column string is a folder and i need to read files from the specified folder e.g Main.txt has Mike 690 Jhon 346 i need to read Mike first then open up the files in folder Mike in the same... (2 Replies)
Discussion started by: shackman66
2 Replies

10. UNIX for Dummies Questions & Answers

grep multiple text files in folder into 1 text file?

How do I use the grep command to take mutiple text files in a folder and make one huge text file out of them. I'm using Mac OS X and can not find a text tool that does it so I figured I'd resort to the BSD Unix CLI for a solution... there are 5,300 files that I want to write to one huge file so... (7 Replies)
Discussion started by: coppertone
7 Replies
Login or Register to Ask a Question