# Concatenate a set of files

**URL:** <https://rubytalk.org/t/concatenate-a-set-of-files/40927>\
**Category:** ruby-talk\
**Created:** [26 September 2007 02:10 UTC](https://rubytalk.org/t/concatenate-a-set-of-files/40927 "2007-09-26T02:10:03Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![eggie5](https://avatars.discourse-cdn.com/v4/letter/e/7c8e57/32.png) [@eggie5](https://rubytalk.org/u/eggie5)\
**Post date:** [26 September 2007 02:10 UTC](https://rubytalk.org/t/concatenate-a-set-of-files/40927/1 "2007-09-26T02:10:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Michael\_Glaesemann](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Michael\_Glaesemann](https://rubytalk.org/u/Michael_Glaesemann)\
**Post date:** [26 September 2007 02:28 UTC](https://rubytalk.org/t/concatenate-a-set-of-files/40927/2 "2007-09-26T02:28:56Z")

</div>

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

Michael Glaesemann  
grzm seespotcode net

> **···**
>
> On Sep 25, 2007, at 21:10 , eggie5 wrote:
> 
> > Any ideas?

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [26 September 2007 02:50 UTC](https://rubytalk.org/t/concatenate-a-set-of-files/40927/3 "2007-09-26T02:50:04Z")

</div>

ruby -e "ARGV.sort!;puts ARGF.read" ??\_file \>file\_set

> **···**
>
> 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?

---

<div class="post-metadata">

**Author:** ![Lloyd\_Linklater](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@Lloyd\_Linklater](https://rubytalk.org/u/Lloyd_Linklater)\
**Post date:** [26 September 2007 04:04 UTC](https://rubytalk.org/t/concatenate-a-set-of-files/40927/4 "2007-09-26T04:04:17Z")

</div>

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|  
&nbsp;&nbsp;f\_in = File.open(f\_name, "r")  
&nbsp;&nbsp;f\_in.each {|f\_str| f.puts(f\_str)}  
&nbsp;&nbsp;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-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Ezra\_Zygmuntowicz](https://avatars.discourse-cdn.com/v4/letter/e/958977/32.png) [@Ezra\_Zygmuntowicz](https://rubytalk.org/u/Ezra_Zygmuntowicz)\
**Post date:** [26 September 2007 07:25 UTC](https://rubytalk.org/t/concatenate-a-set-of-files/40927/5 "2007-09-26T07:25:27Z")

</div>

$ cat \*\_file \> combined\_file

Cheers-  
-- Ezra Zygmuntowicz -- Founder & Ruby Hacker  
-- ez@engineyard.com  
-- Engine Yard, Serious Rails Hosting  
-- (866) 518-YARD (9273)

> **···**
>
> 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?

---

<div class="post-metadata">

**Author:** ![eggie5](https://avatars.discourse-cdn.com/v4/letter/e/7c8e57/32.png) [@eggie5](https://rubytalk.org/u/eggie5)\
**Post date:** [26 September 2007 02:50 UTC](https://rubytalk.org/t/concatenate-a-set-of-files/40927/6 "2007-09-26T02:50:04Z")

</div>

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

Any recommendations?

> **···**
>
> 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

---

<div class="post-metadata">

**Author:** ![eggie5](https://avatars.discourse-cdn.com/v4/letter/e/7c8e57/32.png) [@eggie5](https://rubytalk.org/u/eggie5)\
**Post date:** [26 September 2007 03:00 UTC](https://rubytalk.org/t/concatenate-a-set-of-files/40927/7 "2007-09-26T03:00:12Z")

</div>

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

> **···**
>
> 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

---

<div class="post-metadata">

**Author:** ![Kenneth](https://avatars.discourse-cdn.com/v4/letter/k/94ad74/32.png) [@Kenneth](https://rubytalk.org/u/Kenneth)\
**Post date:** [26 September 2007 04:10 UTC](https://rubytalk.org/t/concatenate-a-set-of-files/40927/8 "2007-09-26T04:10:08Z")

</div>

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, 7:49 pm, William James \<w\_a\_x\_...@yahoo.com\> wrote:
> 
> > ruby -e "ARGV.sort!;puts ARGF.read" ??\_file \>file\_set
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [26 September 2007 08:45 UTC](https://rubytalk.org/t/concatenate-a-set-of-files/40927/9 "2007-09-26T08:45:14Z")

</div>

> 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|  
> &nbsp;&nbsp;f\_in = File.open(f\_name, "r")  
> &nbsp;&nbsp;f\_in.each {|f\_str| f.puts(f\_str)}  
> &nbsp;&nbsp;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|  
&nbsp;&nbsp;my\_files.each {|f\_name|  
&nbsp;&nbsp;&nbsp;&nbsp;File.open(f\_name){|f\_in|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f\_in.each {|f\_str| f\_out.puts(f\_str) }  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;}  
}

> **···**
>
> On Sep 25, 11:04 pm, Lloyd Linklater \<ll...@2live4.com\> wrote:

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [26 September 2007 09:00 UTC](https://rubytalk.org/t/concatenate-a-set-of-files/40927/10 "2007-09-26T09:00:05Z")

</div>

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|  
&nbsp;&nbsp;f.puts my\_files.sort.map{|s| IO.read(s)} }

> **···**
>
> 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|  
> > &nbsp;&nbsp;f\_in = File.open(f\_name, "r")  
> > &nbsp;&nbsp;f\_in.each {|f\_str| f.puts(f\_str)}  
> > &nbsp;&nbsp;f\_in.close  
> > end  
> > f.close

---

<div class="post-metadata">

**Author:** ![Lloyd\_Linklater](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@Lloyd\_Linklater](https://rubytalk.org/u/Lloyd_Linklater)\
**Post date:** [26 September 2007 11:13 UTC](https://rubytalk.org/t/concatenate-a-set-of-files/40927/11 "2007-09-26T11:13:43Z")

</div>

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-forum.com/](http://www.ruby-forum.com/).
