My thoughts as an enterprise Java developer.

Thursday, November 02, 2006

Search and replace across files

Some of my co-workers had their cvs login name changed so we had the problem where every directory checked out from cvs had a CVS subdirectory with a Root file in it that had the old username. The easy way was to delete all checkouts and checkout again but if that would cause the loss of data we needed a different solution.

I handled my machine by doing a Windows search for all Root files, adding them to a new project in Source Insight, then doing a search and replace but others don't have nice editors like that. My machine had over 10,000 Root files so manual processes were out of the question.

For those without a nice editor that can do a replace across files I came up with the following solution:
  1. Install UnxUtils and add the main directory to the path.
  2. Run:

    find . -name Root -print | sed "s/.*/copy & &.new \&\& sed \"s\/jstauffer\/jstauffe\/\" &.new > & \&\& del &.new/" > Root.bat

    Windows has find so I had to make sure that it used the correct find. Basically it finds all Root files, creates a command to copy that file, use sed to do the replace back to the original file, and delete the copy of the file.
  3. Run Root.bat
I know the same could have been achieved in Perl or even Java but is there anything else that would be as quick and easy (granted it isn't easy to understand)?

No comments: