[Cialug] find and replace strig in all files within a folder

Afan Pasalic afan at afan.net
Fri Jun 8 08:53:53 CDT 2012



On 6/8/2012 8:42 AM, Daniel A. Ramaley wrote:
> 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.
>
didn't know about single/double quotes. :(

and, yes, it's an array with values "status" and "values". though, later 
on I found instead "$_SESSION" I can search just "_SESSION", it will do 
well too. and skip complicated part about $.
since it was urgent I had to install old Dreamweaver 2004 because I know 
it can do it. download all files, did change and uploaded files
:D
though, I have to test his too because I know I'm gonna need this one 
day again.

you suggest just add curly braces to my line? that's it?


> 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