From: Paul Lutus Date: 2006-11-02T11:35:08+09:00 Subject: Re: Input file, change data, write to file Paul Lutus wrote: / ... >> I'm a ruby newbie trying to read data from a file, make a few changes, >> and write the output to a file so it can be imported into a MySQL >> database. A correction. I just noticed that you mentioned MySQL, and your output has the date format 2006-07-01, typical of MySQL, something I managed to overlook on the first read. So (note the single changed line): --------------------------------------------------- #!/usr/bin/ruby -w data = [ #01234567890123456789012345678901234567890123456789012 "00123 random text 3.1210/20/200610/21/2006 -3.45", "00253 more text 275.8707/01/200606/12/2006 13.46", "00254 more text 777.3407/01/200606/12/2006 14.47", "00255 more text 555.2107/01/200606/12/2006 15.48" ] out_file = File.open("outfile.txt","w") data.each do |record| fields = [ record[0 .. 4],record[5 .. 17],record[18 .. 23],record[24 .. 33],record[34 .. 43],record[44 .. 51] ] fields[3 .. 4].each do |field| field.gsub!(%r{(\d+)/(\d+)/(\d+)},"\\3-\\1-\\2") end out_record = fields.join("\t") + "\n" out_file.write out_record end out_file.close ------------------------------------------- Output: 00123 random text 3.12 2006-10-20 2006-10-21 -3.45 00253 more text 275.87 2006-07-01 2006-06-12 13.46 00254 more text 777.34 2006-07-01 2006-06-12 14.47 00255 more text 555.21 2006-07-01 2006-06-12 15.48 -- Paul Lutus http://www.arachnoid.com