3

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,

3
  • 2
    If you use an alternate delimiter in sed you won't have to escape all those slashes: sed -e 's|http://...|file://...|g Commented Oct 13, 2010 at 14:51
  • @Dennis - this is one of the best sed hints - useful (as always) because of the ubiquity of the slash! Commented Oct 13, 2010 at 18:49
  • Over in the Perl world, we call that http:\/\/ type of mess "Leaning Toothpick Syndrome". Alternate delimiters are your friend. Commented Oct 13, 2010 at 23:56

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

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
0
find . -name '*.php' -type f -exec sed -i 's|http:\/\/ericbrotto.com|file:\/\/\/Users\/ericbrotto\/Documents\/Portfolio_CV_etc.\/Offline_Portfolio|g' "{}" +;

2 Comments

Thanks! Unfortunately this gives me the error: sed: 1: "./EricBrotto/berit/inde ...": invalid command code . Any ideas?
change your s/// to s|||, then check and escape all your slashes again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.