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."
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 Gray II
···
On Apr 6, 2006, at 9:36 AM, Peter Bailey 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."
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
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)
}
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 Gray 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!
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. .
. .
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.
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.
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?