Snippets are tiny notes I've collected for easy reference.
Create a simple histogram or bar-chart in bash
The following script accepts (via stdin) input like this:
foo 10
bar 21
xyzzy 12
and generates (to stdout) a bar-chart like this:
+-------+----+
| foo | 10 | ##########
| bar | 21 | #####################
| xyzzy | 12 | ############
+-------+----+
where the width of the headings (category and count) is automatically determined by the input values and the width of the bar chart (the number of #
characters) is automatically scaled to fit within the current terminal width.
It also demonstrates a few intermediate level awk features such as passing variables (via the -v
parameter) and variable-width printf
statements (via %*s
).
Published 28 Feb 2018
Snippets are tiny notes I've collected for easy reference.