SED Recursive Search and Replace With Multiple Actions

  • strict warning: Non-static method view::load() should not be called statically in /hermes/walnaweb12a/b57/moo.greydragoncom/nodsw/sites/all/modules/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /hermes/walnaweb12a/b57/moo.greydragoncom/nodsw/sites/all/modules/views/handlers/views_handler_argument.inc on line 744.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /hermes/walnaweb12a/b57/moo.greydragoncom/nodsw/sites/all/modules/views/handlers/views_handler_filter.inc on line 607.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /hermes/walnaweb12a/b57/moo.greydragoncom/nodsw/sites/all/modules/views/handlers/views_handler_filter.inc on line 607.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /hermes/walnaweb12a/b57/moo.greydragoncom/nodsw/sites/all/modules/views/handlers/views_handler_filter_boolean_operator.inc on line 159.
Leeland's picture

About once every 6 months or so I end up needing to do a recursive search and replace across a whole bunch of files. Every time I need to do this I turn to SED as it is just so good at doing the work. But of course there is the problem of multiple actions.

The secret to this is using the SED file. This works across thousands of files on the local box and even more interestingly can be wrapped in a SSH script to walk hundreds of remote boxes.

Naturally you need to be careful when doing this. After all one person can really gum up the works, but with a computer you can seriously superglue the whole factory together in a heartbeat.

Quick SED

Put this script into ~/bin/qsed.sh directory, chmod it to 750, and just use it for most stuff.

#!/bin/sh
[ $# -ne 3 ] && \
  echo "error incorrect number of arguments." && \
  echo "Usage: $0 Search-Root Search-Term Replacement-String" && \
  exit 1

[ ! -r $1 ] && \
  echo "error search root $1 is not readable." && \
  echo "Usage: $0 Search-Root Search-Term Replacement-String" && \
  exit 1

for filename in `grep -rl $2 $1`; do
    echo $filename
    sed -i "s/$2/$3/g;" $filename
done

Now quick replaces can be done as simple as:

$ qsed htdocs nodsw.com NODsw.com

That will handle most of the issues. Except of course for the multiple actions. For that you need a control file...

Quick Multiaction SED

First put this script into ~/bin/qmsed.sh directory, and chmod it to 750.

#!/bin/sh
[ $# -ne 3 ] && \
  echo "error incorrect number of arguments." && \
  echo "Usage: $0 Search-Root Search-Term sed-file" && \
  exit 1

[ ! -r $1 ] && \
  echo "error search root $1 is not readable." && \
  echo "Usage: $0 Search-Root Search-Term sed-file" && \
  exit 1

[ ! -r $3 ] && \
  echo "error sed file $3 is not readable." && \
  echo "Usage: $0 Search-Root Search-Term sed-file" && \
  exit 1

for filename in `grep -rl $2 $1`; do
    echo $filename
    sed -f $3 $filename
done

Now multiple action replacements can be done as simple as creating the command file and then running the search like this:

$ cat sedcmds
s/First/--tempFirst--/g
s/Last/First/g
s/--tempFirst--/Last/g
ctrl-d
$ qmsed htdocs nodsw.com sedcmds

Thread Slivers eBook at Amazon