Skip to content

Instantly share code, notes, and snippets.

@eidantoei
Created May 30, 2011 16:48
Show Gist options
  • Save eidantoei/999146 to your computer and use it in GitHub Desktop.
Save eidantoei/999146 to your computer and use it in GitHub Desktop.
Visualizing `uniq -c` result
#!/usr/bin/env ruby
require 'optparse'
bar_string = "|"
bar_max = 30
max = 0
$es_s=["\033[34m","\033[36m","\033[32m","\033[33m","\033[35m","\033[31m"]
$es_b="\033[30m"
$es_e="\033[0m"
$es_u="\033[04m"
prog = File.basename($0, '.*')
usage = <<"EOS"
#{$es_s[0]}#{prog}#{$es_e} - Visualizing `#{$es_s[1]}uniq -c#{$es_e}` result
Usage:
#{$es_s[0]}#{prog}#{$es_e} [#{$es_s[2]}-m#{$es_e} #{$es_u}#{$es_s[3]}max_bars#{$es_e}] [#{$es_s[2]}-b#{$es_e} #{$es_u}#{$es_s[3]}bar_string#{$es_e}] [#{$es_s[2]}-h#{$es_e}]
Example:
cut -f1 -d" " /var/log/messages | sort -n | #{$es_s[1]}uniq -c#{$es_e} | #{$es_s[4]}#{prog}#{$es_e}
cut -f1 -d" " /var/log/messages | sort -n | #{$es_s[1]}uniq -c#{$es_e} | #{$es_s[4]}#{prog}#{$es_e} #{$es_s[2]}-m#{$es_e} #{$es_s[3]}50#{$es_e} #{$es_s[2]}-b#{$es_e} #{$es_s[3]}"+"#{$es_e}
EOS
opt = OptionParser.new
opt.on('-m VAL'){|v|
if v.to_i > 0
bar_max = v.to_i
end
}
opt.on('-b VAL'){|v|
if v.split(//).length == 1
bar_string = v.to_s
end
}
opt.on('-h'){|v|
puts usage
exit 0
}
begin
opt.parse!(ARGV)
rescue
puts prog+": Option invalid."
puts usage
exit 1
end
lines = []
begin
while l = $stdin.gets
$line = l.scan(/^(\s*)([0-9]+) (.*)$/)
$spacer = $line[0][0]
$var = $line[0][1].to_i
$text = $line[0][2]
lines.push({"spacer"=>$spacer,"var"=>$var,"text"=>$text})
if ($var > max)
max = $var
end
end
rescue
puts prog+": invalid string."
exit 1
end
lines.each do |line|
$p = line['var'] * 5 / max
print "#{line['spacer']}"+$es_s[$p]+"#{line['var']}"+$es_e+" "+$es_b+"["+$es_e+$es_s[$p]
$bar_count = bar_max * line['var'] / max
$bar_count.times do |i|
print bar_string
end
$bar_count.upto(bar_max) do |i|
print " "
end
print $es_e+$es_b+"] "+$es_e+"#{line['text']}\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment