File: pawk.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 # pawk [options...] [awk expression...] [files...]
  27 #
  28 # Print AWK expression for each input line: this is a convenient shortcut,
  29 # to type less when using AWK.
  30 #
  31 # If a leading `=` (no quotes) is given as an argument, no inputs are read,
  32 # and the expression is evaluated/printed once, quitting right away.
  33 #
  34 # The options are, available both in single and double-dash versions
  35 #
  36 #   -h, -help             show this help message
  37 #   -ins, -insensitive    match regexes case-insensitively; fail if unsupported
  38 #   -tsv                  tab-separated values: split input fields using tabs
  39 
  40 
  41 case "$1" in
  42     -h|--h|-help|--help)
  43         awk '/^# +pawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  44         exit 0
  45     ;;
  46 esac
  47 
  48 tsv=0
  49 buffered=0
  50 ins=0
  51 no_input=0
  52 cmd='awk'
  53 
  54 while [ $# -gt 0 ]; do
  55     case "$1" in
  56         =) no_input=1; shift; continue ;;
  57 
  58         --b|-buffered|--buffered) buffered=1; shift; continue ;;
  59 
  60         -F)
  61             if [ $# -lt 2 ]; then
  62                 printf "expected value after -F option\n" >&2
  63                 exit 1
  64             fi
  65             cmd="${cmd} -F $2"; shift 2; continue
  66         ;;
  67 
  68         -F*) cmd="${cmd} $1"; shift; continue ;;
  69 
  70         -v)
  71             if [ $# -lt 2 ]; then
  72                 printf "expected variable assignment after -v option\n" >&2
  73                 exit 1
  74             fi
  75             cmd="${cmd} -v $2"; shift 2; continue
  76         ;;
  77 
  78         -i|--i|-ins|--ins|-insensitive|--insensitive) ins=1; shift; continue ;;
  79 
  80         -tsv|--tsv) tsv=1; shift; continue ;;
  81 
  82         -) break ;;
  83 
  84         --) shift; break ;;
  85 
  86         -*)
  87             printf "unsupported option '%s'\n" "$1" >&2
  88             exit 1
  89         ;;
  90     esac
  91 
  92     break
  93 done
  94 
  95 flush=''
  96 if [ "${buffered}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then
  97     flush='fflush()'
  98 fi
  99 
 100 expr="${1:-\$0}"
 101 [ $# -gt 0 ] && shift
 102 
 103 # show all non-existing files given
 104 failed=0
 105 for arg in "$@"; do
 106     [ "${arg}" = "-" ] && continue
 107     [ -e "${arg}" ] && continue
 108     printf "no file named \"%s\"\n" "${arg}" >&2
 109     failed=1
 110 done
 111 
 112 [ "${failed}" -gt 0 ] && exit 2
 113 
 114 ci='
 115     BEGIN {
 116         if (IGNORECASE == "") {
 117             m = "your `awk` command lacks case-insensitive regex-matching"
 118             print(m) > "/dev/stderr"
 119             exit 125
 120         }
 121         IGNORECASE = 1
 122     }
 123 '
 124 if [ "${ins}" -eq 0 ]; then
 125     ci=''
 126 fi
 127 
 128 if [ "${no_input}" -eq 1 ]; then
 129     src="${ci}"'
 130     BEGIN { print ('"${expr}"'); exit }
 131 '
 132 else
 133     src="${ci}"'
 134     { print ('"${expr}"'); '"${flush}"' }
 135 '
 136 fi
 137 
 138 if [ "${tsv}" -eq 1 ]; then
 139     ${cmd} -F "\t" -v OFS="\t" "${src}" "$@"
 140 else
 141     ${cmd} "${src}" "$@"
 142 fi