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