Using sed in shell scripts for multiple files

Hello friendly computer people

This is a two-part question. I am writing two shell scripts with the goal of "finding and replacing" in all index.php files in a folder and its subfolders.

The first shell of the script is this:

echo "running on all directories…"

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

which then runs the search_replace file:

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 as follows:

  • When I run the first script, echos "starts the search and replaces the 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 complete, that is, the words are replaced, and a new htm file is created only once.

  • , .htm .php, index.php ? , .

,

+3
2

search_replace.sh, index.php $1, index.htm $1.htm,

mv $1.htm $1

sed

re:

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

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

Source: https://habr.com/ru/post/1769338/


All Articles