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...] [files...]
  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, -Ffs, -F=fs    make `fs` the field separator
  40 #
  41 # The other options are, available both in single and double-dash versions
  42 #
  43 #   -h, -help    show this help message
  44 #   -i, -ins     match regexes case-insensitively; may fail the default `awk`
  45 #   -tsv         split fields using tabs, same as using -F "\t"
  46 
  47 
  48 tsv=0
  49 ins=0
  50 command='awk'
  51 
  52 while [ $# -gt 0 ]; do
  53     case "$1" in
  54         -F)
  55             if [ $# -lt 2 ]; then
  56                 printf "expected value after -F option\n" >&2
  57                 exit 1
  58             fi
  59             command="${command} -F $2"; shift 2; continue
  60         ;;
  61 
  62         -F*) command="${command} $1"; shift; continue ;;
  63 
  64         -h|--h|-help|--help)
  65             awk '/^# +tawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  66             exit 0
  67         ;;
  68 
  69         -i|--i|-ins|--ins|-insensitive|--insensitive)
  70             ins=1; shift; continue
  71         ;;
  72 
  73         -tsv|--tsv) tsv=1; shift; continue ;;
  74 
  75         -v)
  76             if [ $# -lt 2 ]; then
  77                 printf "expected variable assignment after -v option\n" >&2
  78                 exit 1
  79             fi
  80             command="${command} -v $1"; shift 2; continue
  81         ;;
  82 
  83         -) break ;;
  84 
  85         --) shift; break ;;
  86 
  87         -*)
  88             printf "unsupported option '%s'\n" "$1" >&2
  89             exit 1
  90         ;;
  91     esac
  92 
  93     break
  94 done
  95 
  96 code="${1:-\$0}"
  97 [ $# -gt 0 ] && shift
  98 
  99 # show all non-existing files given
 100 failed=0
 101 for arg in "$@"; do
 102     [ "${arg}" = "-" ] && continue
 103     [ -e "${arg}" ] && continue
 104     printf "no file named \"%s\"\n" "${arg}" >&2
 105     failed=1
 106 done
 107 
 108 [ "${failed}" -gt 0 ] && exit 2
 109 
 110 ci='
 111     BEGIN {
 112         if (IGNORECASE == "") {
 113             m = "your `awk` command lacks case-insensitive regex-matching"
 114             print(m) > "/dev/stderr"
 115             exit 125
 116         }
 117         IGNORECASE = 1
 118     }
 119 '
 120 if [ "${ins}" -eq 0 ]; then
 121     ci=''
 122 fi
 123 
 124 src="${ci}"'
 125     {
 126         v = ('"${code}"')
 127         if (!tally[v]++) keys[++n] = v
 128     }
 129 
 130     END {
 131         for (i = 1; i <= n; i++) {
 132             k = keys[i]
 133             printf "%d\t%s\n", tally[k], k
 134         }
 135     }
 136 '
 137 
 138 printf "tally\tvalue\n"
 139 if [ "${tsv}" -eq 1 ]; then
 140     ${command} -F "\t" "${src}" "$@"
 141 else
 142     ${command} "${src}" "$@"
 143 fi | sort -t "$(printf "\t")" -k1,1