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 from the `awk` 30 # expression given. Results are tab-separated lines each with a tally and 31 # its respective common transformed value, starting with a header line, 32 # where the data lines are backward-sorted by tally, highest to lowest. 33 # 34 # When not given an expression, the default is to use whole lines/records. 35 # A `.` (no quotes) for the expression also enables the default behavior. 36 # 37 # The handy case-insensitive shortcut options may cause this tool to fail, 38 # if the main `awk` tool installed doesn't support the special `IGNORECASE` 39 # variable. 40 # 41 # The AWK options available only in single-dash versions are 42 # 43 # -F fs, -Ffs, -F=fs make `fs` the field separator 44 # 45 # The other options are, available both in single and double-dash versions 46 # 47 # -h, -help show this help message 48 # -i, -ins match regexes case-insensitively; may fail `awk` 49 # -tsv split fields using tabs, same as using -F "\t" 50 51 52 tsv=0 53 ins=0 54 cmd='awk' 55 56 while [ $# -gt 0 ]; do 57 case "$1" in 58 -F) 59 if [ $# -lt 2 ]; then 60 printf "%s: expected value after -F option\n" "$0" >&2 61 exit 1 62 fi 63 cmd="${cmd} -F $2"; shift 2; continue 64 ;; 65 66 -F*) cmd="${cmd} $1"; shift; continue ;; 67 68 -h|--h|-help|--help) 69 awk '/^# +tawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 70 exit 0 71 ;; 72 73 -i|--i|-ins|--ins|-insensitive|--insensitive) 74 ins=1; shift; continue 75 ;; 76 77 -tsv|--tsv) tsv=1; shift; continue ;; 78 79 -v) 80 if [ $# -lt 2 ]; then 81 printf "%s: missing variable assignment after -v option\n" \ 82 "$0" >&2 83 exit 1 84 fi 85 cmd="${cmd} -v $1"; shift 2; continue 86 ;; 87 88 -) break ;; 89 90 --) shift; break ;; 91 92 -*) 93 printf "%s: unsupported option %s\n" "$0" "$1" >&2 94 exit 1 95 ;; 96 esac 97 98 break 99 done 100 101 code="${1:-\$0}" 102 [ $# -gt 0 ] && shift 103 if [ "${code}" = '.' ]; then 104 code='$0' 105 fi 106 107 # show all non-existing files given 108 failed=0 109 for arg in "$@"; do 110 [ "${arg}" = '-' ] && continue 111 [ -e "${arg}" ] && continue 112 printf "%s: no file named \"%s\"\n" "$0" "${arg}" >&2 113 failed=1 114 done 115 116 [ "${failed}" -gt 0 ] && exit 2 117 118 ci=' 119 BEGIN { 120 if (IGNORECASE == "") { 121 m = "your `awk` command lacks case-insensitive regex-matching" 122 print(m) > "/dev/stderr" 123 exit 125 124 } 125 IGNORECASE = 1 126 } 127 ' 128 if [ "${ins}" -eq 0 ]; then 129 ci='' 130 fi 131 132 src="${ci}"' 133 { 134 V = ('"${code}"') 135 if (!TALLY[V]++) KEYS[++N] = V 136 } 137 138 END { 139 for (i = 1; i <= N; i++) { 140 k = KEYS[i] 141 printf "%d\t%s\n", TALLY[k], k 142 } 143 } 144 ' 145 146 printf "tally\tvalue\n" 147 if [ "${tsv}" -eq 1 ]; then 148 ${cmd} -F "\t" "${src}" "$@" 149 else 150 ${cmd} "${src}" "$@" 151 fi | sort -t "$(printf "\t")" -k1,1