Text replacement in multiple files using sed and find

A common task in programming routine is to change variable names, methods in multiple files. This can be performed by a refactor operation in the most IDEs (eclipse, netbeans). However if you have many text files, such as parameter files, configuration files you can do it in a single command in shell:

$ find . -name "*.c" -exec sed -i "s/oldWord/newWord/g" '{}' \;

Explaining:

  1. find . -name “*.txt”: search all files with extension “.txt”, starting from current directory (.)
  2. -exec sed -i : edit file (option -i) with name received from find
  3. “s/oldWord/newWord/g”: replace all ocurrences “oldWord” by “newWord”
  4. ‘{}’: list of file names retrieved from find
  5. \;: end of exec command (it allow mutiple commands)

See more:
http://how-to.wikia.com/wiki/Howto_use_find_and_sed_to_make_a_replace_in_multiple_files

See ya

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: