Dimitri Aivaliotis wrote:
> Hi Peter,
> 
> On 4/6/06, Peter Bailey <pbailey / bna.com> wrote:
>> Hello,
>> I spent my $40 on "Programming Ruby," but, it seems dedicated to C
>> programmer rocket scientists. I work in publishing and basically I just
>> need to open files, convert the data, and write the files, either to the
>> same file or to another. Can someone please help me to write a simple
>> structure to read a file "text1.txt", do regexes and stuff in it to
>> convert data, and then write the output to "text2.txt."
> 
> A very general way to do this is:
> 
> big = 10485760 # we'll deal with files larger than 10MB specially
> 
> read = "text1.txt" # the file to be read
> 
> write = "text2.txt" # the file to be written
> 
> File.open( write, "w") do |w|
>   File.open( read ) do |r|
>     if File.size( read ) < big
>       while line = r.gets
> 	# do somethin with line
>         w << line
>       end
>     else# read the file in big-sized chunks
>       while !f.eof?
>         f.read( big ).split($/).each do |line|
>           # do somethin with line
>           w << line
>         end
>       end
>     end
>   end
> end
> 
> - Dimitri


Dimitri,
I kind of did what you had here, just a bit simpler:

Dir.chdir("C:/scripts/temp")
read    =   "eula.txt"
write   =   "test1.txt"
File.open(write, "w") do |w|
    File.open(read) do |r|
        while line = r.gets
        line.gsub(/Microsoft/, "Apple")
        w << line
        end
    end
end

It works. I get my file, "test1.txt," but, no changes were made to the 
text. All the Microsofts are still Microsoft, and not Apple.

-Peter

-- 
Posted via http://www.ruby-forum.com/.