Question

I am trying to search for all files on my server called index.php and then use xargs to run sed to replace some text in those files.

The line I want to find is:

echo '<form method="get" class="search_form" action="'.$siteUrl.'">';

And after the replacement, it should look like this:

echo '<form method="get" class="search_form" action="/">';

Therefore, I really only need to change '.$siteUrl.' to a single /

However, when I try to run this command from an SSH prompt (which I think is right):

find . -name "index.php" -print | xargs sed -i 's/\'\.\$siteUrl\.\'/"/"/g'

it just drops me to a prompt of > and does not go any further...

I have also tried using the same sed command on a single file, which does the same. Can someone point me out where I have gone wrong?

Was it helpful?

Solution

  • You are replacing text with / which is also used as the substitution delimiter. Escape it like this: \/
  • No need to put double quotes around the replacement since they are already present in the html string.
  • Use double quotes around the entire sed expression, to avoid conflict with '
  • -print is the default action for find and can be skipped.

Try this:

find . -name "index.php" | xargs sed -i "s/'\.\$siteUrl\.'/\//g"

Or use a different delimiter for sed, like : and remove the escape before /:

find . -name "index.php" | xargs sed -i "s:'\.\$siteUrl\.':/:g"

Oh, and always a good idea to save a backup of edited files (-i.bak):

find . -name "index.php" | xargs sed -i.bak "s:'\.\$siteUrl\.':/:g"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top