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