concatenate a set of files

E

eggie5

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?
 
W

William James

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?

ruby -e "ARGV.sort!;puts ARGF.read" ??_file >file_set
 
E

eggie5

What have you tried? What in particular are you having trouble with?

Michael Glaesemann
grzm seespotcode net

I'm just looking for recommendations, because I have no idea where to
start...

Any recommendations?
 
L

Lloyd Linklater

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. :)
 
S

SpringFlowers AutumnMoon

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

Ezra Zygmuntowicz

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?


$ cat *_file > combined_file


Cheers-
-- Ezra Zygmuntowicz
-- Founder & Ruby Hacker
-- (e-mail address removed)
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)
 
W

William James

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

disclaimer: 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) }
}
}
}
 
W

William James

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

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)} }
 
L

Lloyd Linklater

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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top