lib/rake/data/csv.rb
##
# Find the unique values of a column in a csv file
Find the unique values of a column in a csv file
Find the unique values of a column in a csv file
def unique_values dest, src, columns
  file dest => src do |task|
    mkdir_p task.to_s.pathmap( "%d" )
    values = {}
    col_names = columns.split( /,/ )
    col_nums = nil
    CSV.open( task.source ).each do |row|
      if col_nums.nil?
        col_nums = {}
        col_names.each do |n|
          col_nums[n] = row.index n
          raise "Couldn't find column #{column} in #{src}" unless col_nums[n]
        end
      else
        key = col_names.collect { |x| row[col_nums[x]] }.join( "," )
        values[key] = true
      end
    end
    File.open( task.to_s, "wb" ) do |out|
      out.puts columns
      values.keys.each do |val|
        out.puts val
      end
    end
  end
end
def unique_values dest , src , columns file dest => src do | task | mkdir_p task . to_s . pathmap ( "%d" ) values = { } col_names = columns . split ( / , / ) col_nums = nil CSV . open ( task . source ) . each do | row | if col_nums . nil? col_nums = { } col_names . each do | n | col_nums [ n ] = row . index n raise "Couldn't find column #{column} in #{src}" unless col_nums [ n ] end else key = col_names . collect { | x | row [ col_nums [ x ] ] } . join ( "," ) values [ key ] = true end end File . open ( task . to_s , "wb" ) do | out | out . puts columns values . keys . each do | val | out . puts val end end end end
Find the unique values of a column in a csv file
Find the unique values of a column in a csv file
dest
src
columns