Monday, July 12, 2010

mass replacement of text in files

UPDATE: Phil pointed out there was an easier way to get mass replacement of text, Kudos mate.
find ./ -type f | grep -v svn  | xargs perl -pi -e "s/partA/partB/g;s/partC/partD/g;s/partE/partF/g"

Here is a crazy bit of script that uses a pair of seds to replace a several pieces of text across all files in the directory and below.

#example script that converts the following
#  _address => _address1
#  _address_street => _address2
#  _address_building => _address3
find ./ -type f | grep -v svn | sed "s/\(.*\)/sed 's|_address_building|_address3|g' \1 > \1.script; mv \1.script \1/" | sh
find ./ -type f | grep -v svn | sed "s/\(.*\)/sed 's|_address_street|_address2|g' \1 > \1.script; mv \1.script \1/" | sh
find ./ -type f | grep -v svn | sed "s/\(.*\)/sed 's|_address|_address1|g' \1 > \1.script; mv \1.script \1/" | sh
find ./ -type f | grep -v svn | sed "s/\(.*\)/sed 's|_address12|_address2|g' \1 > \1.script; mv \1.script \1/" | sh
find ./ -type f | grep -v svn | sed "s/\(.*\)/sed 's|_address13|_address3|g' \1 > \1.script; mv \1.script \1/" | sh

2 comments:

  1. Ash,

    perl and xargs are your friends. The regexps are nicer too.

    perl -pi -e "s///g" file

    find ... | xargs perl -pi -e "s///g;s///g;s///g"

    edits the file in place, old version is put in file.bak

    ReplyDelete
  2. Thanks man. perl -pi is really much more convenient for this a mass find and replace task. And I do use it too.

    However the post was because I have recently been working with tasks that locate one piece of information that is the key to taking a second action hence the paired sed regexps.

    I figured that applying it to simple mass search and replace task would make it easier to understand but I guess I wasn't clear about why I was using the sed's to do it in the first place.

    One of my recent usages of it was to login to a group servers with what should be the same web app running in all nodes, the inner sed builds a series of commands that find and md5's all the files in the web app on each node and dumps this to a file. Then I just diff the files and it tells me if any one of the apps are off and which file is wrong... And its all 1 (nightmare-ish) line on my local shell to boot.

    ReplyDelete