Reputation: 35
I'm looking to run script or command to mass change a string that is a URL. I've viewed many examples on this forum, however none are working.
I have created a .sh file to run the following:
$SRC='$url = "https://www.myurl.com/subdir/process.do"';
$DST='$url="https://api.myurl.com/subdir/process.do"';
find . -type f -name "*.php" -exec sed -i 's/$SRC/$DST/g' {} +;
This is not working. I'm thinking it may because of having backslashes in the search content? The search/replace is needed to be run across all sub-directories on .php files.
Any assistance would be greatly appreciated.
Thanks!
Upvotes: 1
Views: 2950
Reputation: 468
First thing - check your variable definitions. In bash, variable definitions usually do not start with a leading $. Ie, should be:
SRC='$url = "https://www.myurl.com/subdir/process.do"';
DST='$url="https://api.myurl.com/subdir/process.do"';
Next, you should switch to using single quotes for the pattern, and double quotes for the variable, as per: https://askubuntu.com/questions/76808/how-do-i-use-variables-in-a-sed-command
Example that seems to work:
sed -i 's,'"$SRC"','"$DST"','
UPDATE: This exact script works perfectly for me on Linux:
#!/bin/bash
SRC='$url = "https://www.myurl.com/subdir/process.do"';
DST='$url="https://api.myurl.com/subdir/process.do"';
find . -type f -name "*.php" -exec sed -i 's,'"$SRC"','"$DST"',' {} \;
Contents of file "asdf.php" created in home directory (before running script):
$url = "https://www.myurl.com/subdir/process.do"
Contents of file "asdf.php" after running script:
$url="https://api.myurl.com/subdir/process.do"
Upvotes: 1