Skip to content

Instantly share code, notes, and snippets.

@andy318
Created April 25, 2013 18:26
Show Gist options
  • Save andy318/5461940 to your computer and use it in GitHub Desktop.
Save andy318/5461940 to your computer and use it in GitHub Desktop.
Read the lines of a csv file and extract a certain field and list the unique values in that field and sort that list based on how many times a value is repeated
cdrs = File.readlines ARGV[0]
cdrs.shift 1 # Delete the header row
cdrs.map! { |x| x.split(',')[3] } # Keep only the callid puts
duplicate_callids = cdrs.group_by(&:freeze). # Group by occurence
sort_by { |k, v| v.length}.reverse. # Sort by number of occurences desc
map { |x| "#{x[0]} - #{x[1].length}" if x[1].length > 1 }. # Format for printing compact
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment