Sha256: 1a6227abae692d5d6a4b2bc24e15bd0137f03f3eace1258e2e238d060846a153

Contents?: true

Size: 800 Bytes

Versions: 16

Compression:

Stored size: 800 Bytes

Contents

#!/usr/bin/env ruby
require 'csv'

def columns_changed?(previous_row, row, change_columns)
  changed = false
  change_columns.each do |column|
    changed ||= previous_row[column] != row[column]
  end
  changed
end

def file_name_from_changed_columns(row, change_columns)
  change_columns.map { |column| row[column] }.join("_") + ".csv"
end

change_columns = ARGV[0].split(/,/).map(&:to_i)
input_file = ARGV[1]

previous_row = output_csv = nil
CSV.foreach(input_file) do |row|
  if previous_row.nil? || (previous_row && columns_changed?(previous_row, row, change_columns))
    output_csv.close if output_csv
    output_file_name = file_name_from_changed_columns(row, change_columns)
    output_csv = CSV.open(output_file_name, "w")
  end
  output_csv << row
  previous_row = row
end
output_csv.close

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
advance-0.3.2 bin/csv_split_on_change_nh.rb
advance-0.3.1 bin/csv_split_on_change_nh.rb
advance-0.3.0 bin/csv_split_on_change_nh.rb
advance-0.2.1 bin/csv_split_on_change_nh.rb
advance-0.2.0 bin/csv_split_on_change_nh.rb
advance-0.1.13 bin/csv_split_on_change_nh.rb
advance-0.1.12 bin/csv_split_on_change_nh.rb
advance-0.1.9 bin/csv_split_on_change_nh.rb
advance-0.1.8 bin/csv_split_on_change_nh.rb
advance-0.1.7 bin/csv_split_on_change_nh.rb
advance-0.1.6 bin/csv_split_on_change.rb
advance-0.1.5 bin/csv_split_on_change.rb
advance-0.1.4 bin/csv_split_on_change.rb
advance-0.1.3 bin/csv_split_on_change.rb
advance-0.1.1 bin/csv_split_on_change.rb
advance-0.1.0 bin/csv_split_on_change.rb