tags:

views:

44

answers:

2

Hello friendly computing people,

This is a two part question. I'm writing two shell scripts with the goal of doing a 'find and replace' in all the index.php files in a folder and its subfolders.

The first shell script is this:

echo "running on all directories…"

find . -name '*.php' -exec ./search_replace.sh {} \;

which then runs the file search_replace :

echo "running search and replace script..."

sed -e 's/http:\/\/ericbrotto.com/file:\/\/\/Users\/ericbrotto\/Documents\/Portfolio_CV_etc.\/Offline_Portfolio/g' index.php > index.htm

My problems/questions are these:

  1. When I run the first script it echos "running search and replace script..." 13 times which makes sense because there are 13 index.php files in the folders. But the result is that only one file is completed i.e. the words are replaced and an new htm file is created only once.

  2. Is there anyway to do this so that instead of creating a new .htm file based on the .php file, I can just have the same index.php file with the words replaced? In other words can they edit the file without having to create a new one.

Thanks so much,

A: 

Since the full pathname is sent to your search_replace.sh, you could replace index.php with $1, index.htm with $1.htm and then

mv $1.htm $1

as the line after your sed

re: your comments

original

sed -e 's/http:\/\/ericbrotto.com/file:\/\/\/Users\/ericbrotto\/Documents\/Portfolio_CV_etc.\/Offline_Portfolio/g' index.php > index.html

the lines s/b

sed -e 's/http:\/\/ericbrotto.com/file:\/\/\/Users\/ericbrotto\/Documents\/Portfolio_CV_etc.\/Offline_Portfolio/g' $1 > $1.html
mv $1.html $1
KevinDTimm
Thanks for your contribution, but after writing this to my second shell script, I got no results:sed -e 's/http:\/\/ericbrotto.com/file:\/\/\/Users\/ericbrotto\/Documents\/Portfolio_CV_etc.\/Offline_Portfolio/g' mv $1.htm $1
Eric Brotto
Did you implement my updates?
KevinDTimm
A: 
find . -name '*.php' -type f -exec sed -i 's|http:\/\/ericbrotto.com|file:\/\/\/Users\/ericbrotto\/Documents\/Portfolio_CV_etc.\/Offline_Portfolio|g' "{}" +;
ghostdog74
Thanks! Unfortunately this gives me the error: sed: 1: "./EricBrotto/berit/inde ...": invalid command code . Any ideas?
Eric Brotto
change your `s///` to `s|||`, then check and escape all your slashes again.
ghostdog74