Skip to content

Instantly share code, notes, and snippets.

@nadeem-khan
Last active August 29, 2015 14:06
Show Gist options
  • Save nadeem-khan/fadd89ddfeb04365b9ea to your computer and use it in GitHub Desktop.
Save nadeem-khan/fadd89ddfeb04365b9ea to your computer and use it in GitHub Desktop.
Script to create columns in CSV file based on values of two other CSV files
csvmain = CSV.read('data-cleaned.csv', headers:true);nil
csvsku = CSV.read('spree_products_sku.csv', headers:true)
new_rows = [csvmain.headers]
index = 0
index_parsed = 0
start_index = 0
indices = []
csvsku.each do |row|
not_found = true
index = start_index
while(index < csvmain.size && not_found) do
  if row['SKU'].strip != csvmain[index]['name'].strip
#puts "ignoring index: #{index} id: #{row['id']} #{row['SKU']} != #{csvmain[index]['name']}" if index_parsed == 135 and     index < 200
  else
puts "writing row: #{csvmain[index]}" if index < 10
    new_row = csvmain[index]

    new_row['id'] = row['id']
new_row['sale-price'] = "sale-price: #{new_row['sale-price']}"

puts "writing row: #{new_row} with id: #{new_row['id']}" if index < 10
    new_rows << new_row
    not_found = false
  end
  index += 1
end
   if(index >= csvmain.size)
puts "couldn't find record for: #{row['id']}"	
  else
  #start_index = index #search for records after this
  start_index = 0 #search for records after this
   end
indices << index_parsed
index_parsed += 1
  end
     puts "indices parsed: #{indices.size}"
  new_file = CSV.open('data-update.csv', 'wb') do |csv|
   new_rows.each do |new_row|
      csv << new_row
   end
   end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment