[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

concatenate a set of files

eggie5

9/26/2007 2:07:00 AM

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?

10 Answers

Michael Glaesemann

9/26/2007 2:29:00 AM

0


On Sep 25, 2007, at 21:10 , eggie5 wrote:

> Any ideas?

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

Michael Glaesemann
grzm seespotcode net



William James

9/26/2007 2:49:00 AM

0

On Sep 25, 9:07 pm, eggie5 <egg...@gmail.com> wrote:
> 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

eggie5

9/26/2007 2:49:00 AM

0

On Sep 25, 7:28 pm, Michael Glaesemann <g...@seespotcode.net> 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 Glaesemann
> grzm seespotcode net

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

Any recommendations?

eggie5

9/26/2007 2:59:00 AM

0

On Sep 25, 7:49 pm, William James <w_a_x_...@yahoo.com> wrote:
> On Sep 25, 9:07 pm, eggie5 <egg...@gmail.com> wrote:
>
> > 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

that's the craziest thing I've seen in my life! I'll give it a go
though, thanks.

Lloyd Linklater

9/26/2007 4:04:00 AM

0

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. :)
--
Posted via http://www.ruby-....

SpringFlowers AutumnMoon

9/26/2007 4:10:00 AM

0

On Sep 25, 7:49 pm, William James <w_a_x_...@yahoo.com> 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.

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

Ezra Zygmuntowicz

9/26/2007 7:25:00 AM

0


On Sep 25, 2007, at 7:10 PM, eggie5 wrote:

> 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
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)



William James

9/26/2007 8:45:00 AM

0

On Sep 25, 11:04 pm, Lloyd Linklater <ll...@2live4.com> 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.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) }
}
}
}

William James

9/26/2007 8:59:00 AM

0

On Sep 25, 11:04 pm, Lloyd Linklater <ll...@2live4.com> 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"]
> 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)} }

Lloyd Linklater

9/26/2007 11:14:00 AM

0

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. :)
--
Posted via http://www.ruby-....