{"id":4553,"date":"2016-08-30T16:56:15","date_gmt":"2016-08-30T15:56:15","guid":{"rendered":"https:\/\/stevepedwards.today\/DebianAdmin\/?p=4553"},"modified":"2023-10-28T22:04:06","modified_gmt":"2023-10-28T21:04:06","slug":"printing-ascii-histograms-from-a-numeric-data-file","status":"publish","type":"post","link":"https:\/\/stevepedwards.today\/DebianAdmin\/printing-ascii-histograms-from-a-numeric-data-file\/","title":{"rendered":"Printing (LIVE!) ASCII Histograms from Numeric Data Output + Files"},"content":{"rendered":"<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_4553\" class=\"pvc_stats all  \" data-element-id=\"4553\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p><center><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/9HI3gNoyY7k?autoplay=1&amp;version=3&amp;loop=1&amp;playlist=9HI3gNoyY7k\" width=\"460\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/center>&nbsp;<\/p>\n<p><strong><span style=\"color: #0000ff;\">Live histogram in a terminal??!! So how did I get to this point after 3 days of hard slog? Read on..!<\/span><\/strong><\/p>\n<p><strong>2\/9\/16 OK, I have checked my suspicion that the number of hashes are out of proportion\u00a0for live data after the first reference maximum is read (without the sort function in the first part of the original function) if smaller than later numbers.\u00a0You can change\u00a0the multiplier to 1 from 60, for exact proportion, but then your ranges are limited to about 1-143 max without spilling onto new lines for a full screen terminal width. <\/strong>Programmers out there will have solutions no doubt...<\/p>\n<p><a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/143max.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-4662\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/143max.png\" alt=\"143max.png\" width=\"1362\" height=\"131\" \/><\/a><\/p>\n<p>Other limitations are data cannot be 0 or negative numbers, due to divide by zero errors.<\/p>\n<p><strong>-\u00a0<\/strong><\/p>\n<p>Boy! This topic had me going bonkers! I could not find simple examples on the web over 2 days for terminal ASCII histograms! Making sense of Perl nearly got me sectioned...and I still failed to get it's methods...Awk gave me hope, as I did not think it possible to create these histos in awk without perl as I never found an example until this <strong><em>seemingly<\/em><\/strong> complex nightmare, which is the command that the above statements and limitations above refer to, when altered for the live data output without the sort section:<\/p>\n<p><span style=\"color: #0000ff;\">history | awk '{h[$2]++}END{for(i in h){print h[i],i|\"sort -rn|head -20\"}}' |awk '!max{max=$1;}{r=\"\";i=s=60*$1\/max;while(i--&gt;0)r=r\"#\";printf \"%15s %5d %s %s\",$2,$1,r,\"\\n\";}'<\/span><\/p>\n<p><a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/histhisto.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-4554\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/histhisto.png\" alt=\"histhisto.png\" width=\"706\" height=\"437\" \/><\/a><\/p>\n<p>Many people on the web want this too, as it's very useful, without installing large job specific Apps, so why such a lack of simple examples or good tutorials out there? I guess it's because awk and perl are complex to understand, and if you just want a simple diagram from some numbers, who is going to start learning these langs from scratch when other tools like Gnuplot are simpler to use? You can see the journey I took on the awk\/perl notepad page - though I'm trying to learn awk anyway.<\/p>\n<p>It turns out, that after fighting with various web examples, then simplifying them as best I could via trial and error mostly, trying to work out the arcane dark art that is perl, I had an answer from day 1 staring me in the face, with just one tweak required - \"print $1\" - for it to process a simple numeric data file and spit out bars:<\/p>\n<p><span style=\"color: #0000ff;\">perl -pe 's\/(\\d+)$\/\"=\"x$1\/e; print \"$1 \"' random.txt<\/span><\/p>\n<p><a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/randomhisto.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-4555\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/randomhisto.png\" alt=\"randomhisto.png\" width=\"802\" height=\"267\" \/><\/a><\/p>\n<p>Easy eh? Yeah, once shown..!<\/p>\n<p>Now there is fine tuning for the pedantic, as padding is required for single digits, accounting for decimals etc. but it is a start, and can be tidied via sort if required:<\/p>\n<p><span style=\"color: #0000ff;\">perl -pe 's\/(\\d+)$\/\"=\"x$1\/e; print \"$1 \"' random.txt | sort -nr<\/span><\/p>\n<p><a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/randsorted.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-4565\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/randsorted.png\" alt=\"randsorted.png\" width=\"802\" height=\"267\" \/><\/a><\/p>\n<p>The main point is that it READS EACH LINE of the file without loops etc. - default general behaviour for awk as I've seen so far - but had me stumped with the main example I was working with below, which has a \"multiplier\" function to add the \"=\" bar characters in proportion to the data, for scaling the\u00a0decimal to a better view size:<\/p>\n<p><span style=\"color: #0000ff;\">\u00a0perl -e 'while(1) {`uptime` =~ \/average: ([\\d.]+)\/; printf(\"% 5s %s\\n\", $1, \"#\" x ($1 * 10)); sleep 3 }'<\/span><\/p>\n<p><a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/perlloadavg-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-4559\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/perlloadavg-1.png\" alt=\"perlloadavg.png\" width=\"802\" height=\"216\" \/><\/a><\/p>\n<p>You can see why I had problems trying to reduce this example to something simpler to take file input rather than a command, and keep the bar output, AND remove the while loop and sleep timer. It would not work, whatever I tried, as it only reads the first line from an input and stops, even if awk was trying to feed it something like:<\/p>\n<p><span style=\"color: #0000ff;\">awk '{print}' 1_5.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">1<\/span><br \/>\n<span style=\"color: #ff0000;\"> 2<\/span><br \/>\n<span style=\"color: #ff0000;\"> 3<\/span><br \/>\n<span style=\"color: #ff0000;\"> 4<\/span><br \/>\n<span style=\"color: #ff0000;\"> 5<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk '{print}' 1_5.txt | perl -e '{\/^\/ =~ \/([\\d.]+)\/; printf(\"% 5s %s\\n\", $1, \"#\" x ($1 * 10)) }'<\/span><br \/>\n<span style=\"color: #ff0000;\">1 #<\/span><\/p>\n<p>One other major mistake I kept making was confusing \"backticks\" for my usual single quotes:<\/p>\n<p><strong>`command`<\/strong>\u00a0is required - NOT<strong> 'command'<\/strong><\/p>\n<p>These keys are accessed via my Fn key with the \"\u00ac marked\" key (next to key1), the Spacebar...I've not used them before as most stuff is pasted from elsewhere, so if you are using single quotes, and your commands are not working how they should, you know why, as in the example:<\/p>\n<p>{<strong>`uptime`<\/strong> =~ \/average:<\/p>\n<p>I did get <span style=\"color: #0000ff;\">cat\u00a01_5.txt <span style=\"color: #ffffff;\">to work in the\u00a0place of <span style=\"color: #0000ff;\">uptime<\/span>, but perl still only reads one line for multi line examples:<\/span><\/span><\/p>\n<p><span style=\"color: #0000ff;\">perl -e '{`cat load.txt` =~ \/average: ([\\d.]+)\/; printf(\"% 5s %s\\n\", $1, \"#\" x ($1 * 10))}'<\/span><br \/>\n<span style=\"color: #ff0000;\">1.15 ##<\/span><\/p>\n<p>No good for:<\/p>\n<p><span style=\"color: #0000ff;\"> cat 6_1.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">6<\/span><br \/>\n<span style=\"color: #ff0000;\">5<\/span><br \/>\n<span style=\"color: #ff0000;\">4<\/span><br \/>\n<span style=\"color: #ff0000;\">3<\/span><br \/>\n<span style=\"color: #ff0000;\">2<\/span><br \/>\n<span style=\"color: #ff0000;\">1\u00a0<\/span><\/p>\n<p><span style=\"color: #0000ff;\">perl -e '{`cat 6_1.txt` =~ \/([\\d.]+)\/; printf(\"% 5s %s\\n\", $1, \"#\" x ($1 * 10))}'<\/span><br \/>\n<span style=\"color: #ff0000;\">6 <\/span><\/p>\n<p>As only the first line (6) is read, not the rest. That was a frustrating dead end for ages.<\/p>\n<p>With scripting; research, trial, error and persistence is often the only way to get to s solution, short of spending weeks learning a language from scratch in the hope you will cover exactly what you are looking for, but you still need some of the basics of any language to have an idea of behaviour, and character options for specific functions...not easy.<\/p>\n<p>Now with these examples, <strong><em>uptime<\/em><\/strong> particularly, you MAY be able amend it sufficiently to apply to any\/all of the system tools I have listed in the <span style=\"color: #00ff00;\"><a style=\"color: #00ff00;\" href=\"https:\/\/stevepedwards.today\/DebianAdmin\/linux-performance-tools-list-check-utilisation-saturation-errors\/\" target=\"_blank\" rel=\"noopener\">Performance Tools Post<\/a><\/span><\/p>\n<p><strong>31\/8\/16: Ok, 2 days later I have learned a lot by studying the history histogram function above and found some (not all!!) of how it works. Smart programmer did that line! :<\/strong><\/p>\n<p>The function is actually two awk commands that can be treated separately - good!<\/p>\n<p><span style=\"color: #0000ff;\">history | awk '{h[$2]++}END{for(i in h){print h[i],i|\"sort -rn|head -20\"}}'<\/span><br \/>\n<span style=\"color: #ff0000;\">419 awk<\/span><br \/>\n<span style=\"color: #ff0000;\">216 perl<\/span><br \/>\n<span style=\"color: #ff0000;\">122 cat<\/span><br \/>\n<span style=\"color: #ff0000;\">39 vi...<\/span><\/p>\n<p>The first above is easy-ish - loops to find how many occurrences of each command used, sorts high to low numerically, then heads it to only 20. (The full history record varies on setup and linux dist. Mint default list is:<\/p>\n<p><span style=\"color: #0000ff;\">history | wc -l<\/span><br \/>\n<span style=\"color: #ff0000;\">1000<\/span><\/p>\n<p>The history awk\u00a0function\u00a0reads $2 for the command names and puts them in h[$2] :<\/p>\n<p><span style=\"color: #0000ff;\">history | tail -3<\/span><br \/>\n<span style=\"color: #ff0000;\">2208 <strong>wc -w<\/strong> &lt; TheRavenV1_3.txt <\/span><br \/>\n<span style=\"color: #ff0000;\">2209 <strong>history<\/strong> | head -3<\/span><br \/>\n<span style=\"color: #ff0000;\">2210 <strong>history<\/strong> | tail -3<\/span><\/p>\n<p><span style=\"color: #0000ff;\">history | awk '{h[$2]++}END{for(i in h){print h[i],i|\"sort -rn|head -20\"}}' |awk '!max{max=$1;}{r=\"\";i=s=60*$1\/max;while(i--&gt;0)r=r\"#\";printf \"%15s %5d %s %s\",$2,$1,r,\"\\n\";}' | head -2<\/span><br \/>\n<span style=\"color: #ff0000;\"><strong>awk<\/strong> 419 <\/span><br \/>\n<span style=\"color: #ff0000;\"><strong>perl<\/strong> 216 #<\/span><\/p>\n<p>I <em>think<\/em> it puts the contents of $2 of history list into an incrementing array named h[$2], so contains the command name, later ID'd by $2, and h[$2], with the increment integer (i) up to max occurrence ID'd by h[i] and i in the for loop:\u00a0This can be shown by printing the contents of each section:<\/p>\n<p>Array command list contents (each occurrence counted by uniq here for clarity):<\/p>\n<p><span style=\"color: #0000ff;\">history | awk '{h[$2]++; print $2}' | uniq -c | head<\/span><br \/>\n<span style=\"color: #ff0000;\">5 cat<\/span><br \/>\n<span style=\"color: #ff0000;\">3 perl<\/span><br \/>\n<span style=\"color: #ff0000;\">1 uptime<\/span><br \/>\n<span style=\"color: #ff0000;\">1 vi<\/span><br \/>\n<span style=\"color: #ff0000;\">22 cat<\/span><br \/>\n<span style=\"color: #ff0000;\">1 sudo<\/span><br \/>\n<span style=\"color: #ff0000;\">2 perl<\/span><br \/>\n<span style=\"color: #ff0000;\">1 uptime<\/span><br \/>\n<span style=\"color: #ff0000;\">10 cat<\/span><br \/>\n<span style=\"color: #ff0000;\">10 uptime<\/span><\/p>\n<p>Array Command Occurrence Counter:<\/p>\n<p><span style=\"color: #0000ff;\">history | awk '{h[$2]++; print h[$2]}'<\/span><\/p>\n<p><span style=\"color: #ff0000;\">72<\/span><br \/>\n<span style=\"color: #ff0000;\">1<\/span><br \/>\n<span style=\"color: #ff0000;\">73<\/span><br \/>\n<span style=\"color: #ff0000;\">33<\/span><br \/>\n<span style=\"color: #ff0000;\">420<\/span><br \/>\n<span style=\"color: #ff0000;\">421<\/span><br \/>\n<span style=\"color: #ff0000;\">422<\/span><\/p>\n<p>For loop counter contents:<\/p>\n<p><span style=\"color: #0000ff;\">history | awk '{h[$2]++}END{for(i in h){print h[i],i }}'<\/span><br \/>\n<span style=\"color: #ff0000;\">1 rm<\/span><br \/>\n<span style=\"color: #ff0000;\">419 awk<\/span><br \/>\n<span style=\"color: #ff0000;\">1 sudo<\/span><br \/>\n<span style=\"color: #ff0000;\">2 cd<\/span><br \/>\n<span style=\"color: #ff0000;\">3 clear<\/span><br \/>\n<span style=\"color: #ff0000;\">39 vi...<\/span><\/p>\n<p>This also shows I have used only 45 different commands in the whole 1000 size list:<\/p>\n<p><span style=\"color: #0000ff;\">history | awk '{h[$2]++}END{for(i in h){print h[i],i | \"sort -rn\"}}' | wc -l<\/span><br \/>\n45<\/p>\n<p><strong>Summary Part1<\/strong>\u00a0(<em>I think??<\/em>) - output of history command column $2 are stored in a numbered\u00a0incrementing array, counted for occurrence of each command and labelled using a FOR loop, then numerically sorted by occurrence and headed.<\/p>\n<p><strong>Part 2<\/strong><\/p>\n<p><span style=\"color: #0000ff;\">awk '!max{max=$1;}{r=\"\";i=s=60*$1\/max;while(i--&gt;0)r=r\"#\";printf \"%15s %5d %s %s\",$2,$1,r,\"\\n\";}'<\/span><\/p>\n<p>The next awk command can't be shown directly as it needs the prior awk\u00a0output, but knowing the format of the first part that feeds it comprises only 2 columns, a different 2 column input can be fed in to learn it's behaviour. Using cat to give a number list and a name file from prior awk notepad page\u00a0file examples, to give a text section as the history command would;<\/p>\n<p>cat -n acronyms.txt | awk '{print $1,$2}' | <span style=\"color: #0000ff;\">awk '!max{max=$1;}{r=\"\";i=s=60*$1\/max;while(i--&gt;0)r=r\"#\";printf \"%15s %5d %s %s\",$2,$1,r,\"\\n\";}'<\/span><br \/>\n<span style=\"color: #ff0000;\">BASIC 1 <\/span><br \/>\n<span style=\"color: #ff0000;\">CICS 2 <\/span><br \/>\n<span style=\"color: #ff0000;\">COBOL 3 <\/span><\/p>\n<p>Now it is obvious that the multiplier is 60*$1. If reduced to 1, it shows:<\/p>\n<p><span style=\"color: #0000ff;\">cat -n acronyms.txt | awk '{print $1,$2}' | awk '!max{max=$1;}{r=\"\";i=s=1*$1\/max;while(i--&gt;0)r=r\"#\";printf \"%15s %5d %s %s\",$2,$1,r,\"\\n\";}'<\/span><br \/>\n<span style=\"color: #ff0000;\">BASIC 1 # <\/span><br \/>\n<span style=\"color: #ff0000;\">CICS 2 ## <\/span><br \/>\n<span style=\"color: #ff0000;\">COBOL 3 <\/span><br \/>\n<span style=\"color: #ff0000;\">DBMS 4 # <\/span><br \/>\n<span style=\"color: #ff0000;\">GIGO 5 ## <\/span><br \/>\n<span style=\"color: #ff0000;\">GIRL 6 <\/span><br \/>\n<span style=\"color: #ff0000;\">AWK 7 # <\/span><br \/>\n<span style=\"color: #ff0000;\">PERL 8 ## <\/span><br \/>\n<span style=\"color: #ff0000;\">FORTRAN 9 \u00a0<\/span><\/p>\n<p><strong>Summary Part 2\u00a0<\/strong><\/p>\n<p>An experienced programmer would have seen immediately that it is part 2 that is the generic useful part for histogram usage as this contains the multiplier to suit various number ranges, and reads a NOT MAX and MAX value and uses a dividend (somehow!) to scale the output according the maximum and minimum of the range (?? not sure about that or how).<\/p>\n<p>Point is, Part 2 can be used in isolation for many basic number lists - experimenting now...<\/p>\n<p>It works correctly\u00a0on the random.txt file, scaling automatically AND padding single digits properly in a terminal, with the 60 multiplier! Don't know how it works yet...(why 60?) This scales all below by 2\/3 max...very clever...<\/p>\n<p><span style=\"color: #0000ff;\">cat random.txt | awk '{print $1,$2}' | awk '!max{max=$1;}{r=\"\";i=s=60*$1\/max;while(i--&gt;0)r=r\"#\";printf \"%15s %5d %s %s\",$2,$1,r,\"\\n\";}'<\/span><br \/>\n<span style=\"color: #ff0000;\">90 <\/span><br \/>\n<span style=\"color: #ff0000;\">87 # <\/span><br \/>\n<span style=\"color: #ff0000;\">74 ## <\/span><br \/>\n<span style=\"color: #ff0000;\">40 <\/span><br \/>\n<span style=\"color: #ff0000;\">90 <\/span><br \/>\n<span style=\"color: #ff0000;\">70 ## <\/span><br \/>\n<span style=\"color: #ff0000;\">46 # <\/span><br \/>\n<span style=\"color: #ff0000;\">10 # <\/span><br \/>\n<span style=\"color: #ff0000;\">35 <\/span><br \/>\n<span style=\"color: #ff0000;\">\u00a0 8 <\/span><br \/>\n<span style=\"color: #ff0000;\">\u00a0 1 # <\/span><br \/>\n<span style=\"color: #ff0000;\">\u00a0 0<\/span><\/p>\n<p>Generating some random numbers, I get the single digit large multiplier issue:<\/p>\n<p><span style=\"color: #0000ff;\">for x in {1..5}; do echo $((1 + RANDOM % 20)); done\u00a0&gt;&gt; Random.txt<\/span><\/p>\n<p><span style=\"color: #ff0000;\">6<\/span><br \/>\n<span style=\"color: #ff0000;\">19<\/span><br \/>\n<span style=\"color: #ff0000;\">8<\/span><br \/>\n<span style=\"color: #ff0000;\">2<\/span><br \/>\n<span style=\"color: #ff0000;\">11<\/span><\/p>\n<p><span style=\"color: #0000ff;\">cat Random.txt | awk '{print $1,$2}' | awk '!max{max=$1;}{r=\"\";i=s=60*$1\/max;while(i--&gt;0)r=r\"#\";printf \"%15s %5d %s %s\",$2,$1,r,\"\\n\";}'<\/span><br \/>\n<span style=\"color: #ff0000;\"> 2 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 8 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 7 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 6 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 0 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 5 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 0 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 4 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 0 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 6 <\/span><\/p>\n<p>BUT, as soon as you have a double digit in there, it auto formats all lines to x3max total in proportion for all - which for most purposes should be fine if data list has at least a 10 in there!<\/p>\n<p><span style=\"color: #0000ff;\">rm Random.txt<\/span><\/p>\n<p><span style=\"color: #0000ff;\">echo $((1 + RANDOM % 20)) &gt;&gt; Random.txt<\/span><\/p>\n<p><span style=\"color: #0000ff;\">cat Random.txt | awk '{print $1,$2}' | awk '!max{max=$1;}{r=\"\";i=s=60*$1\/max;while(i--&gt;0)r=r\"#\";printf \"%15s %5d %s %s\",$2,$1,r,\"\\n\";}'<\/span><br \/>\n<span style=\"color: #ff0000;\">20 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 5 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 12 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 11 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 20 <\/span><\/p>\n<p>Also, just found out that the 60 is the max number of hashes to be shown for the biggest number in any range - vi told me...<\/p>\n<p><a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/60hashes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-4610\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/60hashes.png\" alt=\"60hashes.png\" width=\"802\" height=\"80\" \/><\/a><\/p>\n<p>An interesting thing to KNOW re awk, I found due to that hash line above; It needs to complete the first line scan...if you want number of chars in a line e.g. by setting the FS (Field Separator) to null, as there are no white spaces between chars:<\/p>\n<p><span style=\"color: #0000ff;\">cat hashes.txt<\/span><\/p>\n<p>The number of hashes won't be read correctly for the first line, only the second! This could be a prob in a program eh? A headscratcher debugging it!<\/p>\n<p><span style=\"color: #0000ff;\">awk 'FS = \"\"; {print NF}' hashes.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">0<\/span><br \/>\n<span style=\"color: #ff0000;\">60<\/span><\/p>\n<p>You get round this by using a BEGIN function before the main awk loop body starts to allow the first line to be read before the loop:<\/p>\n<p><span style=\"color: #0000ff;\">awk 'BEGIN {FS=\"\"} {print NF }' hashes.txt<\/span><br \/>\n<span style=\"color: #ff0000;\">60<\/span><br \/>\n<span style=\"color: #ff0000;\">60<\/span><\/p>\n<p>I suppose the main thing that drove all this was to see Performance Stats on the command line, so as a simple example from log\/recorded data, amend the command to suit. This is some data from the live Gnuplot Posts where $3 is the CPU usr load :<\/p>\n<p><span style=\"color: #0000ff;\">cat dstatlive.txt<\/span><br \/>\n26-08 18:20:12| 13 3 82 2 0 00<br \/>\n26-08 18:20:13| 14 4 81 0 0 10<br \/>\n26-08 18:20:14| 12 4 81 3 0 00<\/p>\n<p>Running this through the command gives the wrong column order, as the 26 is from the file's $1 date:<\/p>\n<p><span style=\"color: #0000ff;\">cat dstatlive.txt | awk '{print <strong>$1,$2<\/strong>}' | awk '!max{max=$1;}{r=\"\";i=s=60*$1\/max;while(i--&gt;0)r=r\"#\";printf \"%15s %5d %s %s\",$2,$1,r,\"\\n\";}'<\/span><br \/>\n<span style=\"color: #ff0000;\">18:20:12| 26 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 18:20:13| 26 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 18:20:14| 26 <\/span><\/p>\n<p>Change $1 to $3:<\/p>\n<p><span style=\"color: #0000ff;\">cat dstatlive.txt | awk '{print <strong>$3,$2<\/strong>}' | awk '!max{max=$1;}{r=\"\";i=s=60*$1\/max;while(i--&gt;0)r=r\"#\";printf \"%15s %5d %s %s\",$2,$1,r,\"\\n\";}'<\/span><br \/>\n<span style=\"color: #ff0000;\">18:20:12| 13 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 18:20:13| 14 ## <\/span><br \/>\n<span style=\"color: #ff0000;\"> 18:20:14| 12 ##<\/span><\/p>\n<p>Here's an answer to help - replacing cat and one awk, and with altered spacers between fields, so now we know what they do...<\/p>\n<p><span style=\"color: #0000ff;\">awk '{print $1,$2,$3}' dstatlive.txt | awk '!max{max=$3;}{r=\"\";i=s=60*$3\/max;while(i--&gt;0)r=r\"#\";printf \" <strong>%1s %1s %2d %s %s\",$1,$2,$3<\/strong>,r,\"\\n\";}'<\/span><br \/>\n<span style=\"color: #ff0000;\">26-08 18:20:12| 13 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 26-08 18:20:13| 14 ## <\/span><br \/>\n<span style=\"color: #ff0000;\"> 26-08 18:20:14| 12 ##<\/span><\/p>\n<p>Try and work out how to include the full date time columns for the data yourself then apply it to any logs you have of the same\/similar format, like sadf generates:<\/p>\n<p>AMDA8 600 <strong>2016-08-31 20:15:01<\/strong> UTC all %%system 3.63<br \/>\nAMDA8 600 2016-08-31 20:15:01 UTC all %%iowait 0.41<br \/>\nAMDA8 600 2016-08-31 20:15:01 UTC all %%steal 0.00<br \/>\nAMDA8 600 2016-08-31 20:15:01 UTC all %%idle 86.99<\/p>\n<p>You have a useful tool there if you can adapt it. Here's an immediate practical example running dstat for 15 secs:<\/p>\n<p><span style=\"color: #0000ff;\">\u00a0dstat -tc | awk '{if(NR&gt;3) print $1,$2,$3,$4 fflush()}' | tee dstat15secs.txt<\/span><\/p>\n<p><span style=\"color: #0000ff;\">awk '{print $1,$2,$3}' dstat15secs.txt | awk '!max{max=$3;}{r=\"\";i=s=60*$3\/max;while(i--&gt;0)r=r\"#\";printf \" %1s %1s %2d %s %s\",$1,$2,$3,r,\"\\n\";}'<\/span><br \/>\n<span style=\"color: #ff0000;\">31-08 23:04:34| 14 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:04:35| 11 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:04:36| 14 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:04:37| 14 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:04:38| 13 ## <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:04:39| 12 # <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:04:40| 14 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:04:41| 13 ## <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:04:42| 13 ## <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:04:43| 13 ## <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:04:44| 15 ## <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:04:45| 11 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:04:46| 15 ## <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:04:47| 12 # <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:04:48| 17 #<\/span><\/p>\n<p><strong>AND FINALLY - for the last trick - Do It Live!! Bypass the files and pipe it directly into the function for a live per seconds, updating histo!! <\/strong>Remember from the Live Data Posts to add the extra $4 so the weird extra 0 that awk adds does not affect the data you want in $3.<\/p>\n<p><span style=\"color: #0000ff;\">dstat -tc | awk '{if(NR&gt;3) print $1,$2,$3,<strong>$4<\/strong> fflush()}' | awk '!max{max=$3;}{r=\"\";i=s=60*$3\/max;while(i--&gt;0)r=r\"#\";printf \" %1s %1s %2d %s %s\",$1,$2,$3,r,\"\\n\";}'<\/span><br \/>\n<span style=\"color: #ff0000;\">31-08 23:13:37| 15 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:13:38| 12 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:13:39| 17 ## <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:13:40| 13 # <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:13:41| 13 # <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:13:42| 15 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:13:43| 13 # <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:13:44| 13 # <\/span><br \/>\n<span style=\"color: #ff0000;\"> 31-08 23:13:45| 14 ##<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><center><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/9HI3gNoyY7k?autoplay=1&amp;version=3&amp;loop=1&amp;playlist=9HI3gNoyY7k\" width=\"460\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/center>&nbsp;<\/p>\n<p>A static example over 2.5 hrs of sadf %user load 10min sample logs since PC start:<\/p>\n<p><span style=\"color: #0000ff;\">sadf | grep user | awk '{print $3,$4,$8 }' | awk '!max{max=$3;}{r=\"\";i=s=60*$3\/max;while(i--&gt;0)r=r\"#\";printf \" %1s %1s %2d %s %s\",$1,$2,$3,r,\"\\n\";}'<\/span><br \/>\n<span style=\"color: #ff0000;\">2016-09-02 09:05:02 11 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 2016-09-02 09:15:01 7 ## <\/span><br \/>\n<span style=\"color: #ff0000;\"> 2016-09-02 09:25:01 8 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 2016-09-02 09:35:01 6 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 2016-09-02 09:45:01 10 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 2016-09-02 09:55:01 13 ## <\/span><br \/>\n<span style=\"color: #ff0000;\"> 2016-09-02 10:05:01 13 ## <\/span><br \/>\n<span style=\"color: #ff0000;\"> 2016-09-02 10:15:01 16 # <\/span><br \/>\n<span style=\"color: #ff0000;\"> 2016-09-02 10:25:01 14 ## <\/span><br \/>\n<span style=\"color: #ff0000;\"> 2016-09-02 10:35:01 14 # <\/span><br \/>\n<span style=\"color: #ff0000;\"> 2016-09-02 10:45:01 11 # <\/span><br \/>\n<span style=\"color: #ff0000;\"> 2016-09-02 10:55:01 11 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 2016-09-02 11:05:01 15 ## <\/span><br \/>\n<span style=\"color: #ff0000;\"> 2016-09-02 11:15:01 17 ##<\/span><br \/>\n<span style=\"color: #ff0000;\"> 2016-09-02 11:25:01 18 <\/span><br \/>\n<span style=\"color: #ff0000;\"> 2016-09-02 11:35:01 14 <\/span><\/p>\n<p><strong>OK, I have checked my suspicion that the number of hashes are incorrect after the first reference maximum, which is why the original function sorts the numbers first, so the maximum is read first and becomes the reference. So you would have to put sort -k=[x.xx] in there before the awk function. Unfortunately, this puts dates out of order AND negates running correct relative values for accurate live data...shame....<\/strong><\/p>\n<p>This one for free memory changes may be useful over 40 secs, as it fills a full term;\u00a0enough to see wild changes anyway..? This is \"live\" but buffered and shown once complete.<\/p>\n<p><span style=\"color: #0000ff;\">free -c 40 -s1 | grep Mem | awk '{print $4 }' | awk '!max{max=$1;}{r=\"\";i=s=140*$1\/max;while(i--&gt;0)r=r\"#\";printf \" %1s %1s %s\",$1,r,\"\\n\";}'<\/span><\/p>\n<p><a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/freememhisto.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-4683\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/freememhisto.png\" alt=\"freememhisto.png\" width=\"1366\" height=\"743\" \/><\/a><\/p>\n<p>This next one below is a comparison of free mem from vmstat, seen against free above;\u00a0They differ by a factor of ten in bytes readings seen below, but show near equivalence in proportion, so accurate enough. It's a lot easier to see changes in a diagram than reading numbers eh?!<\/p>\n<p><span style=\"color: #0000ff;\">sudo vmstat 1 | awk '{if(NR&gt;4) print $4 fflush()}' | awk '!max{max=$1;}{r=\"\";i=s=140*$1\/max;while(i--&gt;0)r=r\"#\";printf \" %1s %1s %s\",$1,r,\"\\n\";}'<\/span><\/p>\n<p><a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/vmstatfree.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-4686\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/vmstatfree.png\" alt=\"vmstatfree.png\" width=\"1366\" height=\"768\" \/><\/a><\/p>\n<p>If you wanted to get 4 systems monitored on 4 small terms, drop the 140 to 70:<\/p>\n<p><span style=\"color: #0000ff;\">sudo vmstat 1 | awk '{if(NR&gt;4) print $4 fflush()}' | awk '!max{max=$1;}{r=\"\";i=s=70*$1\/max;while(i--&gt;0)r=r\"#\";printf \" %1s %1s %s\",$1,r,\"\\n\";}'<\/span><\/p>\n<p><a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/4terms.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-5094\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2016\/08\/4terms.png\" alt=\"4terms.png\" width=\"1366\" height=\"768\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_4553\" class=\"pvc_stats all  \" data-element-id=\"4553\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p>&nbsp; Live histogram in a terminal??!! So how did I get to this point after 3 days of hard slog? Read on..! 2\/9\/16 OK, I have checked my suspicion that the number of hashes are out of proportion\u00a0for live data after the first reference maximum is read (without the sort function in the first part <a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/printing-ascii-histograms-from-a-numeric-data-file\/\" class=\"more-link\">...<span class=\"screen-reader-text\">\u00a0 Printing (LIVE!) ASCII Histograms from Numeric Data Output + Files<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-4553","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"a3_pvc":{"activated":true,"total_views":2,"today_views":0},"_links":{"self":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/posts\/4553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/comments?post=4553"}],"version-history":[{"count":1,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/posts\/4553\/revisions"}],"predecessor-version":[{"id":10054,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/posts\/4553\/revisions\/10054"}],"wp:attachment":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/media?parent=4553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/categories?post=4553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/tags?post=4553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}