From: Keith Fahlgren Date: 2005-07-29T22:34:02+09:00 Subject: Re: Sed -> Ruby : .. and ... Hi all, Thanks for the helpful responses. Many corrected me, rightly, for writing a whole program rather than just using a one-liner and for modifying the file in place rather than setting up temporary files. I was concentrating on my task, making a template to replace thousands of lines of sed files rather than doing a single replacement. My larger problem still revolved around doing a bunch matches inside an range. On Thursday 28 July 2005 5:46 pm, mathew wrote: > working_file.gsub!(/(?=BEGIN RANGE)(.*?)(?=END RANGE)/m) {|| > $1.gsub(/MATCH/m, 'REPLACEMENT') > } Matthew pointed out an easier way to get around it but I had rejected that method from the start because I wanted to be able to nest ranges (like I can in sed). I think the problems with my inital attempts was assuming .read was returning an array rather than a string. Slapping .to_a to the end solved that bit and the program now works as expected, I think. Here's my solution (though I'd still love comments): #!/usr/bin/env ruby files = ARGV files.each do |arg| puts "\nOpening file #{arg}" f = File.open(arg) working_file = f.read.to_a f.close working_file.each do |aline| if aline =~ /BEGIN RANGE/ .. aline =~ /END RANGE/ #nest if lines like above as many times as needed for nested ranges aline.sub!(/MATCH/,'REPLACEMENT') end end puts "\nDoing ACTION in #{arg}" fnew = File.new("#{arg}.tmp", "w") puts "\nWriting #{fnew} now" fnew.print(working_file) fnew.close end Thanks, Keith