Linux Search and Replace

I am trying to do the following search and replace, but for some reason it is not working. I am trying to replace:

require_once('

with

require_once($_SERVER['DOCUMENT_ROOT']/'

These are exact tags (slash and single quote are included).

This is what I tried, but I can't get it to work:

 find ./ -type f -readable -writable -exec sed -i "s/require_once(\'/require_once($_SERVER['DOCUMENT_ROOT'] . \'\//g" {} \; 

What am I doing wrong?

+4
source share
2 answers

You need to escape the characters $ and / :

 sed "s/require_once('/require_once(\$_SERVER['DOCUMENT_ROOT']\/'/g" 
+5
source

You need a backslash - avoid double-quoted dollar signs, otherwise the shell will interpolate the _SERVER environment variable (nonexistent), replacing it with an empty string.

0
source

All Articles