0

I tried to use find -type f -name "*.php" -exec sed -i.bak 's|"$_SERVER[\"DOCUMENT_ROOT\"]./|$_SERVER[\"DOCUMENT_ROOT\"]."/|gI' {} \; to replace

<?php
include("$_SERVER["DOCUMENT_ROOT"]./info/benth_nav_top.php");
include_once("$_SERVER["DOCUMENT_ROOT"]./analyticstracking.php");
?>

to

<?php
include($_SERVER["DOCUMENT_ROOT"]."/info/benth_nav_top.php");
include_once($_SERVER["DOCUMENT_ROOT"]."/analyticstracking.php");
?>

Unfortunately, it did not replace anything.

What did I miss?

Thank you in advance

1
  • 2
    [...] in regexes is special, " isn't.
    – muru
    Commented Jun 20, 2018 at 1:35

1 Answer 1

1

You want to move the double quote from before $_SERVER["DOCUMENT_ROOT"]. to after it.

The sed command to do that in a single file:

sed 's/"\(\$_SERVER\["DOCUMENT_ROOT"\]\.\)/\1"/' file.php

The $, [, ] and . are special in the pattern and has to be escaped (this is the main issue in your code). The expression above captures the part of the string that we want to stay unchanged and replaces it with the same string, but with a " inserted afterwards.

Add g after s/.../.../ to make the change for all the occurrences of the pattern on each line.

To run this on all .php files in or under the current directory:

find . -type f -name '*.php' \
    -exec sed -i.bak 's/"\(\$_SERVER\["DOCUMENT_ROOT"\]\.\)/\1"/' {} ';'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .