{"id":916,"date":"2018-06-11T09:33:07","date_gmt":"2018-06-11T09:33:07","guid":{"rendered":"http:\/\/tech.avant.net\/q\/?p=916"},"modified":"2019-04-30T15:26:52","modified_gmt":"2019-04-30T15:26:52","slug":"bash-histogram","status":"publish","type":"post","link":"https:\/\/tech.avant.net\/q\/bash-histogram\/","title":{"rendered":"bash histogram"},"content":{"rendered":"<p>I would like to generate a streamable histgram that runs in bash. Given an input stream of integers (from stdin or a file), I would like to transform each integer to that respective number of &#8220;#&#8221; up to the length of the terminal window; in other words, 5 would become &#8220;#####&#8221;, and so on.<\/p>\n<p>You can get the maximum number of columns in your current terminal using the following command,<\/p>\n<pre class=\"sh_c\">twarnock@laptop: :) tput cols\n143\n<\/pre>\n<p>The first thing we&#8217;ll want to do is create a string of &#8220;####&#8221; that is exactly as long as the max number of columns. I.e.,<\/p>\n<pre class=\"sh_c\">COLS=$(tput cols);\nMAX_HIST=`eval printf '\\#%.0s' {1..$COLS}; echo;`\n<\/pre>\n<p>We can use the following syntax to print a substring of MAX_HIST to any given length (up to its maximum length).<\/p>\n<pre class=\"\">twarnock@laptop: :) echo ${MAX_HIST:0:5}\n#####\ntwarnock@laptop: :) echo ${MAX_HIST:0:2}\n##\ntwarnock@laptop: :) echo ${MAX_HIST:0:15}\n###############\n<\/pre>\n<p>We can then put this into a simple shell script, in this case <strong>printHIST.sh<\/strong>, as follows,<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"Shell\">#! \/bin\/bash\nCOLS=$(tput cols);\nMAX_HIST=`eval printf '\\#%.0s' {1..$COLS}; echo;`\n\nwhile read datain\ndo\n  if [ -n \"$datain\" ]; then\n    echo -n ${MAX_HIST:0:$datain}\n    if [ $datain -gt $COLS ]; then\n      printf \"\\r$datain\\n\"\n    else\n      printf \"\\n\"\n    fi\n  fi\ndone &lt; \"${1:-\/dev\/stdin}\"\n<\/pre>\n<p>This script will also print any number on top of any line that is larger than the maximum number of columns in the terminal window.<\/p>\n<p>As is, the script will transform an input file into a crude histogram, but I&#8217;ve also used it as a visual ping monitor as follows (note the use of unbuffer),<\/p>\n<pre class=\"\">twarnock@cosmos:~ :) ping $remote_host | unbuffer -p awk -F'[ =]' '{ print int($10) }' | unbuffer -p printHIST.sh\n######\n#####\n########\n######\n##\n####\n#################\n###\n#####\n#######\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I would like to generate a streamable histgram that runs in bash. Given an input stream of integers (from stdin or a file), I would like to transform each integer to that respective number of &#8220;#&#8221; up to the length of the terminal window; in other words, 5 would become &#8220;#####&#8221;, and so on. You [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,14],"tags":[],"_links":{"self":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/916"}],"collection":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/comments?post=916"}],"version-history":[{"count":10,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/916\/revisions"}],"predecessor-version":[{"id":945,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/916\/revisions\/945"}],"wp:attachment":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/media?parent=916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/categories?post=916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/tags?post=916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}