Snoopy Dog wrote:
> First let me say that I am an absolute Newbie to Ruby.  So please be
> tolerant of my newbie question.
>
> My situation is this.  I am gathering financial data, and am about to
> change data suppliers. I want to "merge" the files from both suppliers
> to have as much data history as possible. I have the data in ASCII
> format in a comma delimited file.
>
> I have the data in the following structre:
>  c:\data\1Original\abc.csv  - a new data file
>  c:\data\2Processed\abc.csb  - the historical file and my processing
> reference
> Each file has the same file structure of:
>   Symbol, Date, Open, High, Low, Close, Volume
>
> I already have a process that references the files in the
> c:\data\processed directory structure.
>
> Currently I have figured out how to walk the directory tree and copy any
> NEW files into the Processed directory.  I am hung up on the merging of
> the files into the processed directory.
>
> Sample files to demonstrate:
>   c:\data\1Original\abc.csv (new data)
>      abc, 20060901, 1.5, 2.1, 1.4, 1.9, 123456
>      abc, 20060902. 1.9, 2.3, 1.8, 2.3, 147454
>
>   c:\data\2Processed\abc.csv (historical)
>      abc, 20010101, 2.1, 2.5, 2.0, 2.45, 254677
>      abc, 20010102. 2.4, 2.6, 2.4, 2.5, 333444
>      .......
>      abc, 20060901, 1.5, 2.1, 1.4, 1.9, 123456
>
> I need to create
>   c:\data\2Processed\abc.csv (historical)
>      abc, 20010101, 2.1, 2.5, 2.0, 2.45, 254677
>      abc, 20010102. 2.4, 2.6, 2.4, 2.5, 333444
>      .......
>      abc, 20060901, 1.5, 2.1, 1.4, 1.9, 123456
>      abc, 20060902. 1.9, 2.3, 1.8, 2.3, 147454
>

dir1 = '1-Original/'
dir2 = '2-Processed/'
def file_to_a s
  File.exist?(s) ?
    IO.read(s).map{|x| x.chomp.split(/\s*,\s*/)} : []
end
Dir[dir1+"*.csv"].each{|full_name|
  bare_name = full_name[ %r{[^/]*$} ]
  ary = file_to_a( full_name ) | file_to_a( dir2 + bare_name )
  File.open( dir2+bare_name, 'w'){|f|
    f.puts ary.sort.map{|x| x.join(", ")} }
}