[Cialug] find and replace strig in all files within a folder
Daniel A. Ramaley
daniel.ramaley at drake.edu
Fri Jun 8 08:42:53 CDT 2012
On 2012-06-07 at 19:35:21, afan at afan.net wrote:
>$find ./ -name "*.php" -exec sed -i "s/foo/bar/g" {} \;
>and it works. But when I replace foo/bar with $_SESSION['status'] /
>$_SESSION['value'] it doesn't work
First of all, you are using double quotes to enclose the string. So,
your shell (bash?) will most likely try to interpret "$_SESSION" as a
shell variable. And if that shell variable is not set, then those parts
will be replaced with empty strings.
What does this display?
$ echo "s/$_SESSION['status']/$_SESSION['value']/g"
Do the "$_SESSION" parts go away? If so, then you have the problem i
mentioned. The solution is to escape the dollar signs, like this:
$ echo "s/\$_SESSION['status']/\$_SESSION['value']/g"
Others have mentioned that you might also need to escape your square
brackets so that sed doesn't interpret them as part of the regex.
Of course, i'm assuming--based on the other responses--that what you
wanted was to match the literal string "$_SESSION['status']". My first
reaction was to think "oh, you're using associative array variables in
your shell, but forgot to put curly braces in the right spots". If
*that* is the real problem, then see if this prints what you expect:
$ echo "s/${_SESSION['status']}/${_SESSION['value']}/g"
That assumes you have an associative array (hash) named _SESSION with
the keys "status" and "value" defined.
__
Daniel A. Ramaley
Network Engineer 2
Dial Center 112, Drake University
2407 Carpenter Ave / Des Moines IA 50311 USA
Tel: +1 515 271-4540
Fax: +1 515 271-1938
E-mail: daniel.ramaley at drake.edu
More information about the Cialug
mailing list