James Gray
3/17/2006 4:50:00 AM
On Mar 16, 2006, at 5:28 PM, Geoff wrote:
> 1. It does not retain the double quotes and I would like it to be able
> to do that.
> 2. On a previous version it was working for just the last column. I
> have tried to adjust it to make it work for any column (at least
> that I
> list in the write(colNum) definition, but it is not working.
I'm pretty sure this does what you want:
Neo:~/Desktop$ ls
csv_filldown.rb test_data.csv
Neo:~/Desktop$ cat test_data.csv
"BegDoc","EndDoc","New"
"Doc1BegDoc","Doc1EndDoc","Test1"
"Doc2BegDoc","",""
"Doc2BegDoc","",""
"Doc3BegDoc","Doc3EndDoc","Test2"
"Doc4BegDoc","",""
"Doc5BegDoc","Doc5EndDoc","New"
Neo:~/Desktop$ cat csv_filldown.rb
#!/usr/local/bin/ruby -w
require "csv"
require "enumerator"
last_row = Array.new
CSV.foreach(ARGV.shift) do |row|
puts( row.enum_for(:each_with_index).map do |cell, index|
if cell.empty?
last_row[index]
else
last_row[index] = cell
end
end.map { |output| "\"#{output}\""}.join(",") )
end
Neo:~/Desktop$ ruby csv_filldown.rb test_data.csv > output.csv
Neo:~/Desktop$ cat output.csv
"BegDoc","EndDoc","New"
"Doc1BegDoc","Doc1EndDoc","Test1"
"Doc2BegDoc","Doc1EndDoc","Test1"
"Doc2BegDoc","Doc1EndDoc","Test1"
"Doc3BegDoc","Doc3EndDoc","Test2"
"Doc4BegDoc","Doc3EndDoc","Test2"
"Doc5BegDoc","Doc5EndDoc","New"
Hope that helps.
James Edward Gray II