Skip to content

Instantly share code, notes, and snippets.

@unfo
Created March 16, 2015 09:33
Show Gist options
  • Save unfo/86621fa7bc7b87cd99f8 to your computer and use it in GitHub Desktop.
Save unfo/86621fa7bc7b87cd99f8 to your computer and use it in GitHub Desktop.
simple awk script for drawing ascii graphs
# Should be used with | sort | uniq -c
# Example input:
# 2 0
# 3 1
# 46 2
# 6 3
# 20 4
# 115 5
function draw_graph() {
awk 'BEGIN { highest= -1 } { sum += $1; items += 1 ; if ($1 > highest) { highest = $1 } ; values[$2] = $1; } END { print highest; for (val in values) { print values[val] " " val } }' | \
awk '{ if (NF == 1) { highest = $1 } else { printf("%d ", $2); for (i = 0; i <= int(80 * ($1 / highest)); i++) { printf("#") } ; printf(" (%d)\n", $1) } }'
}
$ cat triple | awk -F';' '$5 ~ /[0-9]/ { print $5 }' | sort | uniq -c | draw_graph
0 ## (6)
1 ### (9)
2 ################################# (138)
3 ##### (18)
4 ############## (60)
5 ################################################################################# (345)
$ cat original | awk -F';' '$5 ~ /[0-9]/ { print $5 }' | sort | uniq -c | draw_graph
0 ## (2)
1 ### (3)
2 ################################# (46)
3 ##### (6)
4 ############## (20)
5 ################################################################################# (115)
$ wc -l original triple
193 original
579 triple
$ cat original | awk -F';' '$5 ~ /[0-9]/ { print $5 }' | sort | uniq -c
2 0
3 1
46 2
6 3
20 4
115 5
@unfo
Copy link
Author

unfo commented Mar 26, 2015

hourly log hits

awk '{ print $5 " " $1 }' | draw_graph | sort -k1 -n
0 ################################ (1525)
1 ########################## (1237)
2 ####################### (1059)
3 ############################ (1310)
4 ########################################### (2031)
5 #################################### (1726)
6 ######################################### (1921)
7 ################################################################################# (3840)
8 ################################################################ (3039)
9 ############################################## (2161)
10 ######################################### (1928)
11 ##################################### (1736)
12 #################################### (1698)
13 ############################### (1478)
14 ####################################### (1832)
15 ############################## (1420)
16 ############################## (1404)
17 ##################################### (1750)
18 ########################################### (2058)
19 ######################################### (1966)
20 ######################################### (1964)
21 ######################################### (1955)
22 ################################### (1671)
23 ###################################### (1800)

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