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 case_ins=0
  51 no_input=0
  52 cmd='awk'
  53 
  54 while [ $# -gt 0 ]; do
  55     if [ "$1" = "--" ]; then
  56         shift
  57         break
  58     fi
  59 
  60     case "$1" in
  61         =)
  62             no_input=1
  63             shift
  64             continue
  65         ;;
  66 
  67         --b|-buffered|--buffered)
  68             buffered=1
  69             shift
  70             continue
  71         ;;
  72 
  73         -F)
  74             shift
  75             if [ $# -eq 0 ]; then
  76                 printf "expected value after -F option\n" >&2
  77                 exit 1
  78             fi
  79             cmd="${cmd} -F $1"
  80             shift
  81             continue
  82         ;;
  83 
  84         -F*)
  85             cmd="${cmd} $1"
  86             shift
  87             continue
  88         ;;
  89 
  90         -v)
  91             shift
  92             if [ $# -eq 0 ]; then
  93                 printf "expected variable assignment after -v option\n" >&2
  94                 exit 1
  95             fi
  96             cmd="${cmd} -v $1"
  97             shift
  98             continue
  99         ;;
 100 
 101         -i|--i|-ins|--ins|-insensitive|--insensitive)
 102             case_ins=1
 103             shift
 104             continue
 105         ;;
 106 
 107         -tsv|--tsv)
 108             tsv=1
 109             shift
 110             continue
 111         ;;
 112 
 113         -*)
 114             cmd="${cmd} ${arg}"
 115             shift
 116             continue
 117         ;;
 118     esac
 119 
 120     break
 121 done
 122 
 123 flush=''
 124 if [ "${buffered}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then
 125     flush='fflush()'
 126 fi
 127 
 128 expr="${1:-\$0}"
 129 [ $# -gt 0 ] && shift
 130 
 131 # show all non-existing files given
 132 failed=0
 133 for arg in "$@"; do
 134     if [ "${arg}" = "-" ]; then
 135         continue
 136     fi
 137     if [ ! -e "${arg}" ]; then
 138         printf "no file named \"%s\"\n" "${arg}" >&2
 139         failed=1
 140     fi
 141 done
 142 
 143 if [ "${failed}" -gt 0 ]; then
 144     exit 2
 145 fi
 146 
 147 ci='
 148     BEGIN {
 149         if (IGNORECASE == "") {
 150             m = "your `awk` command lacks case-insensitive regex-matching"
 151             print(m) > "/dev/stderr"
 152             exit 125
 153         }
 154         IGNORECASE = 1
 155     }
 156 '
 157 if [ "${case_ins}" -eq 0 ]; then
 158     ci=''
 159 fi
 160 
 161 if [ "${no_input}" -eq 1 ]; then
 162     src="${ci}"'
 163     BEGIN { print ('"${expr}"'); exit }
 164 '
 165 else
 166     src="${ci}"'
 167     { print ('"${expr}"'); '"${flush}"' }
 168 '
 169 fi
 170 
 171 if [ "${tsv}" -eq 1 ]; then
 172     ${cmd} -F "\t" -v OFS="\t" "${src}" "$@"
 173 else
 174     ${cmd} "${src}" "$@"
 175 fi