Skip to main content

Replace across files from the command line

Posted by staufferjames on July 2, 2007 at 12:00 PM PDT

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

Since then I have started to use jEdit for this kind of thing but sometimes jEdit isn't powerful enough (find and grep can tweak the list of files more).

Related Topics >>