Skip to content

Instantly share code, notes, and snippets.

@relistan
Last active December 14, 2015 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save relistan/5028647 to your computer and use it in GitHub Desktop.
Save relistan/5028647 to your computer and use it in GitHub Desktop.
Draw a histogram from a CSV on the command line
#!/usr/bin/env ruby
COLUMNS_TO_USE = 40.0
histo = []
0.upto(9) { |i| histo[i] = 0 }
field_to_parse = ARGV.shift.to_i
ARGF.each do |line|
fields = line.split(/,/)
score = fields[field_to_parse].to_i
histo[(score * 0.1).floor] += 1
end
max = histo.sort.last
scale = COLUMNS_TO_USE / max
puts "\n\n\n"
puts " 0\n ||"
histo.each { |l| print " ||", '=' * (l.to_i * scale), "\n" }
puts 100
puts "\n\n\n"
@relistan
Copy link
Author

Draw a Histogram from a CSV on the Command Line

Sideways ASCII art FTW.

Usage:

$ histo.rb 2 my_csv_file

2 being the column number to grab from the CSV

Looks like:

 0
 ||
 ||=
 ||======
 ||========
 ||============
 ||================
 ||=========
 ||============
 ||===========
 ||========================================
 ||====================================
100

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment