File: tawk.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 2026 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 # tawk [options...] [awk expression...] [filenames...]
  27 #
  28 #
  29 # Tally via AWK group-counts lines using common results of the AWK expression
  30 # given. When not given any, whole lines are used. Results are tab-separated
  31 # lines each with a tally and its respective common transformed value.
  32 #
  33 # The handy case-insensitive shortcut options may cause this tool to fail,
  34 # if the main AWK tool installed doesn't support the special IGNORECASE
  35 # variable.
  36 #
  37 # The AWK options available only in single-dash versions are
  38 #
  39 #   -F fs               use `fs` for the field separator (the `FS` variable)
  40 #   -V                  show the AWK version installed
  41 #   -v var=val          set a variable to a given value
  42 #
  43 # The other options are, available both in single and double-dash versions
  44 #
  45 #   -h, -help             show this help message
  46 #   -ins, -insensitive    match regexes case-insensitively; fail if unsupported
  47 #   -tsv                  split fields using tabs, same as using -F "\t"
  48 
  49 
  50 case "$1" in
  51     -h|--h|-help|--help)
  52         awk '/^# +tawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  53         exit 0
  54     ;;
  55 esac
  56 
  57 command='awk'
  58 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then
  59     command='stdbuf -oL awk'
  60 fi
  61 
  62 case_insensitive=0
  63 
  64 while [ $# -gt 0 ]; do
  65     arg="$1"
  66 
  67     if [ "${arg}" = "--" ]; then
  68         shift
  69         break
  70     fi
  71 
  72     case "${arg}" in
  73         -F)
  74             shift
  75             if [ $# -eq 0 ]; then
  76                 printf "expected value after -F option\n" >&2
  77                 exit 1
  78             fi
  79             command="${command} -F $1"
  80             shift
  81             continue
  82         ;;
  83 
  84         -F*)
  85             command="${command} ${arg}"
  86             shift
  87             continue
  88         ;;
  89 
  90         -v)
  91             shift
  92             if [ $# -eq 0 ]; then
  93                 printf "expected variable assignment after -v option\n" >&2
  94                 exit 1
  95             fi
  96             command="${command} -v $1"
  97             shift
  98             continue
  99         ;;
 100 
 101         -ins|--ins|-insensitive|--insensitive)
 102             case_insensitive=1
 103             shift
 104             continue
 105         ;;
 106 
 107         -tsv|--tsv)
 108             command="${command} -F \"\\t\""
 109             shift
 110             continue
 111         ;;
 112 
 113         -*)
 114             command="${command} ${arg}"
 115             shift
 116             continue
 117         ;;
 118     esac
 119 
 120     break
 121 done
 122 
 123 code="${1:-\$0}"
 124 [ $# -gt 0 ] && shift
 125 
 126 # show all non-existing files given
 127 failed=0
 128 for arg in "$@"; do
 129     if [ "${arg}" = "-" ]; then
 130         continue
 131     fi
 132     if [ ! -e "${arg}" ]; then
 133         printf "no file named \"%s\"\n" "${arg}" > /dev/stderr
 134         failed=1
 135     fi
 136 done
 137 
 138 if [ "${failed}" -gt 0 ]; then
 139     exit 2
 140 fi
 141 
 142 ci='
 143     BEGIN {
 144         if (IGNORECASE == "") {
 145             m = "your `awk` command lacks case-insensitive regex-matching"
 146             printf("\x1b[38;2;204;0;0m%s\x1b[0m\n", m) > "/dev/stderr"
 147             exit 125
 148         }
 149         IGNORECASE = 1
 150     }
 151 '
 152 if [ "${case_insensitive}" -eq 0 ]; then
 153     ci=''
 154 fi
 155 
 156 ${command} -v sortcmd="sort -t '\t' -rnk1,1" "${ci}"'
 157     BEGIN { print "tally\tvalue"; fflush() }
 158 
 159     FNR == 1 { FS = /\t/ ? "\t" : " "; $0 = $0 }
 160 
 161     {
 162         v = '"${code}"'
 163         if (!tally[v]++) ordkeys[++oklen] = v
 164     }
 165 
 166     END {
 167         for (i = 1; i <= oklen; i++) {
 168             k = ordkeys[i]
 169             printf "%d\t%s\n", tally[k], k | sortcmd
 170         }
 171     }
 172 ' "$@"