Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > concatenate a set of files

Reply
Thread Tools

concatenate a set of files

 
 
eggie5
Guest
Posts: n/a
 
      09-26-2007
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?

 
Reply With Quote
 
 
 
 
Michael Glaesemann
Guest
Posts: n/a
 
      09-26-2007

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



 
Reply With Quote
 
 
 
 
William James
Guest
Posts: n/a
 
      09-26-2007
On Sep 25, 9:07 pm, eggie5 <(E-Mail Removed)> 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

 
Reply With Quote
 
eggie5
Guest
Posts: n/a
 
      09-26-2007
On Sep 25, 7:28 pm, Michael Glaesemann <(E-Mail Removed)> 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?

 
Reply With Quote
 
eggie5
Guest
Posts: n/a
 
      09-26-2007
On Sep 25, 7:49 pm, William James <(E-Mail Removed)> wrote:
> On Sep 25, 9:07 pm, eggie5 <(E-Mail Removed)> 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.

 
Reply With Quote
 
Lloyd Linklater
Guest
Posts: n/a
 
      09-26-2007
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-forum.com/.

 
Reply With Quote
 
SpringFlowers AutumnMoon
Guest
Posts: n/a
 
      09-26-2007
On Sep 25, 7:49 pm, William James <(E-Mail Removed)> 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-forum.com/.

 
Reply With Quote
 
Ezra Zygmuntowicz
Guest
Posts: n/a
 
      09-26-2007

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
-- http://www.velocityreviews.com/forums/(E-Mail Removed)
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)



 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      09-26-2007
On Sep 25, 11:04 pm, Lloyd Linklater <(E-Mail Removed)> 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) }
}
}
}

 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      09-26-2007
On Sep 25, 11:04 pm, Lloyd Linklater <(E-Mail Removed)> 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)} }

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenate/De-Concatenate Carlos VHDL 10 10-24-2012 01:04 PM
concatenate several xml files into one big xml/html file? idiotprogrammer XML 4 03-05-2007 06:43 PM
How to concatenate 'like' files in a dir? wilson_work@yahoo.com Perl Misc 7 02-20-2006 03:54 PM
Need to concatenate all files in a dir together into one file and read the first 225 characters from each file into another file. Tony Perl Misc 5 04-19-2004 03:28 PM
concatenate files recursively in win32 Mike James Perl 1 09-19-2003 03:51 PM



Advertisments