XHTML.net

Technology talks by Loïc d’Anterroches

News, articles, PHP, scripts, XHTML/CSS, …

  1. Home
  2. News

Search and search/replace for a string in files

The 2007-08-11 at 15:35 by Loïc d'Anterroches filed under News.

This is more as a personal reminder:

Find all the files recursively containing the string StringToSearch:

find . | xargs grep 'StringToSearch'

Find and replace all the strings in the files (in place replacement):

perl -pi -e "s/OldString/NewString/g;" ./*.py

or to scan one level deeper in the folders:

perl -pi -e "s/OldString/NewString/g;" ./*/*.py

Comments from readers

a bit more said:

Find all the files recursively containing the string StringToSearch:
grep -R . 'StringToSearch'

Find and replace all the strings in the files (in place replacement):
sed -i "s/OldString/NewString/g;" ./*.py

or to scan one level deeper in the folders:
sed -i "s/OldString/NewString/g;" ./*/*.py

Loïc said:

Thanks for these versions, I really need to a look a little bit more at the man pages of sed and grep!

Jeremiah said:

rpl - search and replace utility is great for these tasks.
http://www.laffeycomputer.com/rpl.html

Voice your ideas

It is painless and I try not to kill electrons in the process.


Your email is required but will not be shared nor displayed.


Do you think your comment will force me to write even better stuff next time? If so, you simply rock.


Logo of Plume CMS