Given a set of files:
01_file
02_file
03_file
…
What’s the best way to concatenate their respective text into one file
‘file_set’. The must be loaded in alphabetical order as they are
listed above.
Any ideas?
Given a set of files:
01_file
02_file
03_file
…
What’s the best way to concatenate their respective text into one file
‘file_set’. The must be loaded in alphabetical order as they are
listed above.
Any ideas?
On Sep 25, 2007, at 21:10 , eggie5 wrote:
Any ideas?
What have you tried? What in particular are you having trouble with?
Michael G.
grzm seespotcode net
On Sep 25, 7:28 pm, Michael G. [email protected] wrote:
On Sep 25, 2007, at 21:10 , eggie5 wrote:
Any ideas?
What have you tried? What in particular are you having trouble with?
Michael G.
grzm seespotcode net
I’m just looking for recommendations, because I have no idea where to
start…
Any recommendations?
On Sep 25, 7:49 pm, William J. [email protected] wrote:
‘file_set’. The must be loaded in alphabetical order as they are
listed above.Any ideas?
ruby -e “ARGV.sort!;puts ARGF.read” ??_file >file_set
that’s the craziest thing I’ve seen in my life! I’ll give it a go
though, thanks.
As a ruby nuby I am not yet good enough to golf this, but I went through
the standard books to cobble this together mostly to say that it should
be easy to figure this out. It took me just a few minutes for this so,
if you get the books, it should work for you too.
my_files = [“f:\belfry\1.txt”, “f:\belfry\2.txt”,
“f:\belfry\3.txt”]
f = File.new(“c:\joined.txt”, “a+”)
my_files.each do |f_name|
f_in = File.open(f_name, “r”)
f_in.each {|f_str| f.puts(f_str)}
f_in.close
end
f.close
disclaimer: I apologize for the look and feel of the ‘compiled
language’ approach. I am still a n00b to the ruby way.
On Sep 25, 7:49 pm, William J. [email protected] wrote:
ruby -e “ARGV.sort!;puts ARGF.read” ??_file >file_set
back in the days when our college instructor asked us to write some
program to do something for homework #5… now think about what it is
like if you hand in just one line.
On Sep 25, 9:07 pm, eggie5 [email protected] wrote:
Any ideas?
ruby -e “ARGV.sort!;puts ARGF.read” ??_file >file_set
On Sep 25, 2007, at 7:10 PM, eggie5 wrote:
Any ideas?
$ cat *_file > combined_file
Cheers-
– Ezra Z.
– Founder & Ruby Hacker
– [email protected]
– Engine Y., Serious Rails Hosting
– (866) 518-YARD (9273)
On Sep 25, 11:04 pm, Lloyd L. [email protected] wrote:
As a ruby nuby I am not yet good enough to golf this, but I went through
the standard books to cobble this together mostly to say that it should
be easy to figure this out. It took me just a few minutes for this so,
if you get the books, it should work for you too.my_files = [“f:\belfry\1.txt”, “f:\belfry\2.txt”,
“f:\belfry\3.txt”]
Even under windoze, Ruby lets you use the forward slash
in paths.
f = File.new(“c:\joined.txt”, “a+”)
my_files.each do |f_name|
f_in = File.open(f_name, “r”)
f_in.each {|f_str| f.puts(f_str)}
f_in.close
end
f.closedisclaimer: I apologize for the look and feel of the ‘compiled
language’ approach. I am still a n00b to the ruby way.
Let Ruby close the files for you.
my_files = [“f:/belfry/1.txt”, “f:/belfry/2.txt”,
“f:/belfry/3.txt”]
File.open( “c:/joined.txt”, “w” ){|f_out|
my_files.each {|f_name|
File.open(f_name){|f_in|
f_in.each {|f_str| f_out.puts(f_str) }
}
}
}
On Sep 25, 11:04 pm, Lloyd L. [email protected] wrote:
f_in.each {|f_str| f.puts(f_str)}
f_in.close
end
f.close
If everything will fit in memory at once, then
we can proudly say, “We don’t need no stinkin’ loops!”
my_files = [“f:/belfry/1.txt”, “f:/belfry/2.txt”,
“f:/belfry/3.txt”]
File.open(“c:/joined.txt”,“w”){|f|
f.puts my_files.sort.map{|s| IO.read(s)} }
Nice one, William! As you can clearly see, I am a ruby nuby and still
have the compiled language syndrome. I just got a box and put my very
first ever linux OS on it. (I have never played with linux before.) I
hope to use it to host my own website on it. (using ruby, of course!)
Thanks again for the lesson.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs