Skip to content

Instantly share code, notes, and snippets.

@katef
Last active October 23, 2020 10:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katef/a7599bc731a150aa63740010813fd8f2 to your computer and use it in GitHub Desktop.
Save katef/a7599bc731a150aa63740010813fd8f2 to your computer and use it in GitHub Desktop.
#!/usr/bin/awk -f
function strrep(n, c) {
s = ""
for (i = 0; i < n; i++) {
s = c s
}
return s
}
{
for (i = 1; i <= NF; i++) {
a[$i]++
}
}
END {
for (k in a) {
if (length(k) > max) {
max = length(k)
}
}
for (k in a) {
if (length(sprintf("%u", max)) > width) {
width = length(sprintf("%u", max))
}
}
for (k in a) {
printf("%-*s %*u %s\n", max, k, width, a[k], strrep(a[k], "▪"));
}
}
@katef
Copy link
Author

katef commented Jul 27, 2020

A simple histogram that works the way I want.

; decomment life-utf8.c | ./hist.awk | grep '^[a-z]' | sort -rnk 2 | head -10
unsigned                 29 ▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪
static                   20 ▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪
y                         8 ▪▪▪▪▪▪▪▪
x                         8 ▪▪▪▪▪▪▪▪
void                      8 ▪▪▪▪▪▪▪▪
return                    8 ▪▪▪▪▪▪▪▪
y;                        7 ▪▪▪▪▪▪▪
x;                        6 ▪▪▪▪▪▪
sizeof                    6 ▪▪▪▪▪▪
y)                        5 ▪▪▪▪▪

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