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

Tim Wilson tim_linux at wilson-home.com
Thu Jun 7 22:57:48 CDT 2012


Try this:
find ./ -name "*.php" -exec sed -i
"s/\(\$_SESSION\['\)status\('\]\)/\1value\2/g" {} \;

I prefer to use parentheses, it helps prevent typos.  The "\1" and "\2"
keeps what's enclosed in parentheses.  So "s/1\(2\)3\(4\)5\(6\)/\1\2\3/g"
would turn "123456" into "246", and turn
"s/1\(2\)3\(4\)5\(6\)/-\1-\2-\3-/g" into "-2-4-6-".

On Thu, Jun 7, 2012 at 9:08 PM, Todd Walton <tdwalton at gmail.com> wrote:

> On Thu, Jun 7, 2012 at 7:35 PM, <afan at afan.net> wrote:
> >
> > hi,
> > what would be the easiest but most simplest way to find and replace
> string
> > "foo" with "bar" in all files within a folder (and all sub-folders)?
> >
> > [...]
> >
> > when tried
> > $find ./ -name "*.php" -exec sed -i
> > "s/\$_SESSION['status']/\$_SESSION['value']/g" {} \;
> > didn't do anything
>
> Perhaps it's seeing the brackets as denoting a list of characters.
> From the sed manual:
>
> [list]
> [^list]
>
> Matches any single character in list: for example, [aeiou] matches all
> vowels. A list may include sequences like char1-char2, which matches
> any character between (inclusive) char1 and char2.
>
> A leading ^ reverses the meaning of list, so that it matches any
> single character not in list. To include ] in the list, make it the
> first character (after the ^ if needed), to include - in the list,
> make it the first or last; to include ^ put it after the first
> character.
>
> The characters $, *, ., [, and \ are normally not special within list.
> For example, [\*] matches either ‘\’ or ‘*’, because the \ is not
> special here. However, strings like [.ch.], [=a=], and [:space:] are
> special within list and represent collating symbols, equivalence
> classes, and character classes, respectively, and [ is therefore
> special within list when it is followed by ., =, or :. Also, when not
> in POSIXLY_CORRECT mode, special escapes like \n and \t are recognized
> within list. See Escapes.
>
> --
> Todd
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> http://cialug.org/mailman/listinfo/cialug
>



-- 
Tim
Required reading: http://bccplease.com/


More information about the Cialug mailing list