File: bully.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2024 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the “Software”), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # bully
  27 # Tally and show bullets
  28 #
  29 # Show a reverse-sorted tally of all lines read, where ties are sorted
  30 # alphabetically. In addition, a 3rd column with bullets helps you
  31 # instinctively grasp tallies as quantities relative to each other.
  32 #
  33 # High tally counts will show a lot of bullets, of course, which isn't
  34 # helpful: the regular tally script is better, in those cases.
  35 #
  36 # Output is a 3-item TSV table, which starts with a header line.
  37 
  38 
  39 # handle help options
  40 case "$1" in
  41     -h|--h|-help|--help)
  42         # show help message, extracting the info-comment at the start
  43         # of this file, and quit
  44         awk '/^# +bully/, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  45         exit 0
  46     ;;
  47 esac
  48 
  49 awk -v sortcmd="sort -t '   ' -rnk2 -k1d" '
  50 BEGIN { print "value\ttally\tbullets" }
  51 
  52 { tally[$0]++ }
  53 
  54 END {
  55     # find the max tally, which is needed to build the bullets-string
  56     max = 0
  57     for (k in tally) {
  58         if (max < tally[k]) max = tally[k]
  59     }
  60 
  61     # make enough bullets for all tallies: this loop makes growing the
  62     # string a task with complexity O(n * log n), instead of a naive
  63     # O(n**2), which can slow-down things when tallies are high enough
  64     bullets = "•"
  65     for (n = max; n > 1; n /= 2) {
  66         bullets = bullets bullets
  67     }
  68 
  69     # emit unsorted output lines to the sort cmd, which will emit the
  70     # final reverse-sorted tally lines
  71     for (k in tally) {
  72         s = substr(bullets, 1, tally[k])
  73         printf "%s\t%d\t%s\n", k, tally[k], s | sortcmd
  74     }
  75 }
  76 ' "$@"