Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

replace bits of fields in mysql

// replace bits of fields in mysql

update  `table_name` set field_name = replace(field_name,"oldtext","newtext")

bash shell code to replace text in multiple files, multiple dirs, (but not in SVN dirs)

find . -name          *.php -exec sed -i 's/oldtext/newtext/g' {} \;
find . -name .svn -prune -o -exec sed -i 's/oldtext/newtext/g' {} \;


My understanding:

find files starting with . (local directory)

-name .svn -prune means if you find .svn for a filename (directory name), skip it.

-o OR

-exec execute

sed -i sed is the stream editor. -i means change the files in place.

's/new/old/g' Substitute old text for new text Globally.

{} sends the filenames that find found to sed

\; terminates the -exec from find, and \ escapes it so the shell won't think it means to end its command.


Mass find-replace using sed

Replace some widespread nasty hardcoded strings. 'sed $file > $file' doesn't work so hot so we use a temp file, and also make a backup of the old file.

for i in $(find . -type f); do sed 's/oldstring/newstring/g' $i > $i-tmp; mv $i $i-backup; mv $i-tmp $i; done