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, -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 case "$1" in 47 -h|--h|-help|--help) 48 awk '/^# +cawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 49 exit 0 50 ;; 51 esac 52 53 tsv=0 54 case_insensitive=0 55 command='awk' 56 57 while [ $# -gt 0 ]; do 58 if [ "$1" = "--" ]; then 59 shift 60 break 61 fi 62 63 case "$1" in 64 -f|-F) 65 shift 66 if [ $# -eq 0 ]; then 67 printf "expected value after -F option\n" >&2 68 exit 1 69 fi 70 command="${command} -F $1" 71 shift 72 continue 73 ;; 74 75 -F*) 76 command="${command} $1" 77 shift 78 continue 79 ;; 80 81 -i|--i|-ins|--ins|-insensitive|--insensitive) 82 case_insensitive=1 83 shift 84 continue 85 ;; 86 87 -tsv|--tsv) 88 tsv=1 89 shift 90 continue 91 ;; 92 esac 93 94 break 95 done 96 97 cond="${1:-\$0}" 98 [ $# -gt 0 ] && shift 99 100 # show all non-existing files given 101 failed=0 102 for arg in "$@"; do 103 if [ "${arg}" = "-" ]; then 104 continue 105 fi 106 if [ ! -e "${arg}" ]; then 107 printf "no file named \"%s\"\n" "${arg}" >&2 108 failed=1 109 fi 110 done 111 112 if [ "${failed}" -gt 0 ]; then 113 exit 2 114 fi 115 116 ci=' 117 BEGIN { 118 if (IGNORECASE == "") { 119 m = "your `awk` command lacks case-insensitive regex-matching" 120 print(m) > "/dev/stderr" 121 exit 125 122 } 123 IGNORECASE = 1 124 } 125 ' 126 if [ "${case_insensitive}" -eq 0 ]; then 127 ci='' 128 fi 129 130 src="${ci}"' 131 FNR == 1 { 132 FS = /\t/ ? "\t" : " " 133 $0 = $0 134 } 135 136 '"${cond}"' { count++ } 137 138 END { print count + 0 } 139 ' 140 141 if [ "${tsv}" -eq 1 ]; then 142 ${command} -F "\t" "${src}" "$@" 143 else 144 ${command} "${src}" "$@" 145 fi