Web Hosting Talk







View Full Version : Bash script to recrusivly open files in a folder and replace text?


matt2kjones
09-06-2006, 05:20 PM
Hey there,

Does anyone know of a bash script that will go through files in a folder recursivly, opening each one, searching for a string and replacing it with a different string.

For example.....

our company uses a site which displays mobile phones to our sales staff so that they can read about phone functions ect (they sell phones)

Now the phoblem is, we had 100+ staff hitting this site every day... sometimes 20 users at once.... all day

The site blocked us, but they have agreed to let us download a copy of their site via wget recusive and save it on our intranet.

Well, the site files come down fine.... but the problem is, they have a different url for images, than they do for html content, for example:

html content: www.example.com
images: img1.example.com

now i have got wget to download all the html files from the domain and replaced the www.example.com with our intranet url, but for the images... all the paths to the images still point to img1.example.com

now we want to replace img1.example.com to the url of the images on our intranet but i cant seem to do this with wget

so i need to run a script on all the html files (around 500 files) and replace the path of the images

any ideas?

thanx

Cirrostratus
09-06-2006, 05:29 PM
#!/bin/sh

for F in `find /home/sites/foodomain.com/ -type f -name '*.html' -print`; do mv $F $F.tmp ; sed 's/<title>Untitled Document<\/title>/<title> My New Site Title <\/title>/g' $F.tmp > $F ; rm $F.tmp ; done



Here is one I used in the past for changing the title in html pages. With a few tweaks and different search path used for 'find' it should do what your looking for.

Thanks,

Jeremy