File: cawk.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 # cawk [options...] [awk condition...] [files...] 27 # 28 # 29 # Count with AWK: count the times the AWK expression/condition given is true. 30 # 31 # The handy case-insensitive shortcut options may cause this tool to fail, 32 # if the main AWK tool installed doesn't support the special IGNORECASE 33 # variable. 34 # 35 # The AWK options available only in single-dash versions are 36 # 37 # -F fs, -Ffs, -F=fs make `fs` the field separator 38 # 39 # The other options are, available both in single and double-dash versions 40 # 41 # -h, -help show this help message 42 # -i, -ins match regexes case-insensitively; may fail the default `awk` 43 # -tsv split fields using tabs, same as using -F "\t" 44 45 46 tsv=0 47 ins=0 48 command='awk' 49 50 while [ $# -gt 0 ]; do 51 case "$1" in 52 -F) 53 if [ $# -lt 2 ]; then 54 printf "expected value after -F option\n" >&2 55 exit 1 56 fi 57 command="${command} -F $2"; shift 2; continue 58 ;; 59 60 -F*) command="${command} $1"; shift; continue ;; 61 62 -h|--h|-help|--help) 63 awk '/^# +cawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 64 exit 0 65 ;; 66 67 -i|--i|-ins|--ins|-insensitive|--insensitive) ins=1; shift; continue ;; 68 69 -tsv|--tsv) tsv=1; shift; continue ;; 70 71 -v) 72 if [ $# -lt 2 ]; then 73 printf "expected variable assignment after -v option\n" >&2 74 exit 1 75 fi 76 command="${command} -v $1"; shift 2; continue 77 ;; 78 79 -) break ;; 80 81 --) shift; break ;; 82 83 -*) 84 printf "unsupported option '%s'\n" "$1" >&2 85 exit 1 86 ;; 87 esac 88 89 break 90 done 91 92 cond="${1:-1}" 93 [ $# -gt 0 ] && shift 94 95 # show all non-existing files given 96 failed=0 97 for arg in "$@"; do 98 [ "${arg}" = "-" ] && continue 99 [ -e "${arg}" ] && continue 100 printf "no file named \"%s\"\n" "${arg}" >&2 101 failed=1 102 done 103 104 [ "${failed}" -gt 0 ] && exit 2 105 106 ci=' 107 BEGIN { 108 if (IGNORECASE == "") { 109 m = "your `awk` command lacks case-insensitive regex-matching" 110 print(m) > "/dev/stderr" 111 exit 125 112 } 113 IGNORECASE = 1 114 } 115 ' 116 if [ "${ins}" -eq 0 ]; then 117 ci='' 118 fi 119 120 src="${ci}"' 121 BEGIN { n = c = count = 0 } 122 ('"${cond}"') { n = c = ++count } 123 END { print count + 0 } 124 ' 125 126 if [ "${tsv}" -eq 1 ]; then 127 ${command} -F "\t" "${src}" "$@" 128 else 129 ${command} "${src}" "$@" 130 fi