concatenate a set of files

Discussion in 'Ruby' started by eggie5, Sep 26, 2007.

  1. eggie5

    eggie5 Guest

    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?
     
    eggie5, Sep 26, 2007
    #1
    1. Advertising

  2. 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
     
    Michael Glaesemann, Sep 26, 2007
    #2
    1. Advertising

  3. On Sep 25, 9:07 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?


    ruby -e "ARGV.sort!;puts ARGF.read" ??_file >file_set
     
    William James, Sep 26, 2007
    #3
  4. eggie5

    eggie5 Guest

    On Sep 25, 7:28 pm, Michael Glaesemann <> 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, Sep 26, 2007
    #4
  5. eggie5

    eggie5 Guest

    On Sep 25, 7:49 pm, William James <> wrote:
    > On Sep 25, 9:07 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?

    >
    > 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.
     
    eggie5, Sep 26, 2007
    #5
  6. 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/.
     
    Lloyd Linklater, Sep 26, 2007
    #6
  7. On Sep 25, 7:49 pm, William James <> 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/.
     
    SpringFlowers AutumnMoon, Sep 26, 2007
    #7
  8. 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
    --
    -- Engine Yard, Serious Rails Hosting
    -- (866) 518-YARD (9273)
     
    Ezra Zygmuntowicz, Sep 26, 2007
    #8
  9. On Sep 25, 11:04 pm, Lloyd Linklater <> 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, Sep 26, 2007
    #9
  10. On Sep 25, 11:04 pm, Lloyd Linklater <> 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)} }
     
    William James, Sep 26, 2007
    #10
  11. 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/.
     
    Lloyd Linklater, Sep 26, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike James
    Replies:
    1
    Views:
    1,834
    Jim Gibson
    Sep 19, 2003
  2. idiotprogrammer
    Replies:
    4
    Views:
    1,147
    Joseph Kesselman
    Mar 5, 2007
  3. Tony
    Replies:
    5
    Views:
    750
  4. Replies:
    7
    Views:
    163
    A. Sinan Unur
    Feb 20, 2006
  5. Carlos

    Concatenate/De-Concatenate

    Carlos, Oct 12, 2012, in forum: VHDL
    Replies:
    10
    Views:
    899
Loading...

Share This Page