わけあって、あるディレクトリ(サブディレクトリを含む)にある全てのファイルの内容の正規表現に一致した部分を置換するプログラムを作ってみた。コマンドライン引数で、対称となる文字列(正規表現)、置換後の文字列、対象のディレクトリ(省略可、デフォルトカレントディレクトリ)、対象となる拡張子(省略可、デフォルトは拡張子の有無に関わらず全てのファイル)となる。Rubyで作る必要があったのかはわからないけど作ってしまった物はしかたがない
def replace_all(target, replace, path, extension) Dir::foreach(path) do |name| next if name == '.' or name == '..' if path =~ /\/$/ name = path + name else name = path + '/' + name end if FileTest::file?(name) && name =~ /#{extension}/ contents = ''; open(name, 'r') do |file| contents = file.read() contents.gsub!(/#{target}/, replace) end open(name, 'w') do |file| file.write(contents); end end if FileTest::directory?(name) replace_all(target, replace, name) end end end if ARGV.length < 2 puts "usage: ruby #{$PROGRAM_NAME} replace_target replace_after [directory] [extension]" exit() end path = './' extension = '.+' if(ARGV.length > 2) path = ARGV[2] end if(ARGV.length > 3) extension = ARGV[3] extension = '\.' + extension + '$' end replace_all(ARGV[0], ARGV[1], path, extension)