Basic input/output question


#1

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.”

Thank you very much.


#2

On Apr 6, 2006, at 9:36 AM, Peter B. 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.”

Sure, see if something like this would help you:

File.open(“text2.txt”, “w”) do |output|
File.foreach(“text1.txt”) do |line|
# change line as need here, for example:
# line.sub!(/regex here/, “replacement here”)…

 output << line

end
end

Hope that helps.

James Edward G. II


#3

Hi Peter,

On 4/6/06, Peter B. removed_email_address@domain.invalid 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

#4

Hi,

On 4/6/06, Peter B. removed_email_address@domain.invalid 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.”

Thank you very much.

If your files aren’t very large, the simple way is to:

data = File.read(“text1.txt”)
result_data = do_stuff_to( data )

open text2.txt in "w"rite mode

File.open(“text2.txt”, “w”){|text2|
text2.write(result_data)
}


#5

Ilmari H. wrote:

Hi,

On 4/6/06, Peter B. removed_email_address@domain.invalid 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.”

Thank you very much.

If your files aren’t very large, the simple way is to:

data = File.read(“text1.txt”)
result_data = do_stuff_to( data )

open text2.txt in "w"rite mode

File.open(“text2.txt”, “w”){|text2|
text2.write(result_data)
}

Thank you! This is great stuff, from all you guys. You’re all heroes. .
. .


#6

James G. wrote:

On Apr 6, 2006, at 9:36 AM, Peter B. 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.”

Sure, see if something like this would help you:

File.open(“text2.txt”, “w”) do |output|
File.foreach(“text1.txt”) do |line|
# change line as need here, for example:
# line.sub!(/regex here/, “replacement here”)…

 output << line

end
end

Hope that helps.

James Edward G. II

I think that this will help me a lot. Now, I see that this is line
oriented. Correct? So, line by line, I can read the lines, change stuff,
and write the lines. Cool. Thanks a lot, James!


#7

Dimitri A. wrote:

Hi Peter,

On 4/6/06, Peter B. removed_email_address@domain.invalid 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


#8

Hi Peter,

On 4/6/06, Peter B. removed_email_address@domain.invalid wrote:

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

    line.gsub(/Microsoft/, "Apple")

Here’s where you need a ! -> that line should read:

       line.gsub!(/Microsoft/,"Apple")

(See James’ example.)

The “gsub” will act on “line”, and return a new object. On the other
hand, the “gsub!” will change the “line” itself, without creating a
new object. This is what you want in this case.

In general, when there’s an exclamation point version of a method, it
will change the object itself, instead of returning a new, changed
object.

  • Dimitri

#9

On 4/6/06, Peter B. removed_email_address@domain.invalid wrote:

Cool. Thanks! So, in James’ example, he uses “sub” instead of “gsub.”
Are they the same thing?

“sub” will only replace the first occurrence of the pattern, whereas
“gsub” will replace all occurrences of the pattern in that string.

  • Dimitri

#10

Dimitri A. wrote:

Hi Peter,

On 4/6/06, Peter B. removed_email_address@domain.invalid wrote:

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

    line.gsub(/Microsoft/, "Apple")

Here’s where you need a ! -> that line should read:

       line.gsub!(/Microsoft/,"Apple")

(See James’ example.)

The “gsub” will act on “line”, and return a new object. On the other
hand, the “gsub!” will change the “line” itself, without creating a
new object. This is what you want in this case.

In general, when there’s an exclamation point version of a method, it
will change the object itself, instead of returning a new, changed
object.

  • Dimitri

Cool. Thanks! So, in James’ example, he uses “sub” instead of “gsub.”
Are they the same thing?


#11

Dimitri A. wrote:

On 4/6/06, Peter B. removed_email_address@domain.invalid wrote:

Cool. Thanks! So, in James’ example, he uses “sub” instead of “gsub.”
Are they the same thing?

“sub” will only replace the first occurrence of the pattern, whereas
“gsub” will replace all occurrences of the pattern in that string.

  • Dimitri

Thanks, Dimitri. Actually, I figured that out on the way home–the “sub”
versus “gsub.” There are some things I have managed to get out my $40
book!