I am a huge fan of CodeWright, a code editor owned by Borland that is no longer being worked on. I have been using it since the beginning of my career, and really fell in love a couple of features that I have been hard pressed to find elsewhere. The first is a relatively minor feature, but comes in handy none-the-less: columnar/verticle selects. This feature allows you to select and edit a block of text, but only the columns you want. This is unbelievably handy when you have a large file that needs columns to be changed.
The other feature that really makes CodeWright a necessary part of my toolkit (up until now :)) was multi-file Find/Replace. CodeWright would let you select a directory (any directory, not just one in your workspace! take that Eclipse!) and any set of files in that directory (recursively or not) and replace a search string (could be a RegExp) with a replacement string. It also lets you approve the changes for each line, so you could surgically make changes with a broadsword, if you so choose.
I use this feature fairly often, but since leaving ACI in May, I have been without a good tool for that job. CodeWright, though no longer supported by Borland, still (STILL!) costs $299 as part of the “Borland Classics” line!!!!! What the #$^! is wrong with this picture? Borland, please PLEASE PLEASE either make it cheap (under $100) or better yet, give to the Open Source community. I would gladly help support this fine tool.
Anyway, enough ranting… back to the purpose of this post! I needed a find/replace tool today for converting ERwin Oracle SQL scripts into MySQL scripts, did not want to do it by hand more than once (which I did yesterday… yuck!).
So, pulling out my handy dandy “Programming Ruby” HTML Help file (left the book at home) I went to work on a script to do it for me. Here it is:
-
#
-
# Find / Replace -
-
# 1.) Moves current file to filename.ext.bak
-
# 2.) Creates a new file "filename.ext" and
-
# 3.) adds each row, first substuting where necessary
-
#
-
# ARGV[0] = regex pattern to find (i.e. "\w*." )
-
# ARGV[1] = replacement text
-
# ARGV[2..] = filename or (i.e.) **\*.txt to hit every .txt file in the dir and its children
-
-
puts “Replacing #{ARGV[0]} with “#{ARGV[1]}" in file(s):"
-
-
# find the regex pattern in the input REGEX
-
regexp = Regexp.new(ARGV[0]);
-
-
for i in 2…ARGV.length
-
Dir[ARGV[i]].each do |path|
-
puts path
-
bakpath = “#{path}.bak”
-
File.rename(path, bakpath)
-
-
File.open( bakpath, “r” ) do |fin|
-
fout = File.open(path, “w”)
-
fin.each do |line|
-
tmp = line.gsub(regexp) { “#{ARGV[1]}” }
-
fout.write tmp
-
end
-
fout.close
-
end
-
end
-
end
Unfortunately, WordPress 2.0 does not like source code anymore and took out my whitespace… oh well. Hopefully that will be fixed soon.
Anyway, the above code is simple, clear, and does exactly what I need it to do. The only thing that is missing is the approval, which I may create a rubyTK program to handle, but this will work for now.