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 to transform all input lines/records. 30 # 31 # When not given an expression, the default is to use whole lines/records. 32 # A `.` (no quotes) for the expression also enables the default behavior. 33 # 34 # The options are, available both in single and double-dash versions 35 # 36 # -h, -help show this help message 37 # -i, -ins match regexes case-insensitively; may fail `awk` 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 buf=0 50 ins=0 51 cmd='awk' 52 53 while [ $# -gt 0 ]; do 54 case "$1" in 55 -b|--b|-buffered|--buffered) buf=1; shift; continue ;; 56 57 -F) 58 if [ $# -lt 2 ]; then 59 printf "%s: expected value after -F option\n" "$0" >&2 60 exit 1 61 fi 62 cmd="${cmd} -F $2"; shift 2; continue 63 ;; 64 65 -F*) cmd="${cmd} $1"; shift; continue ;; 66 67 -v) 68 if [ $# -lt 2 ]; then 69 printf "%s: missing variable assignment after -v option\n" \ 70 "$0" >&2 71 exit 1 72 fi 73 cmd="${cmd} -v $2"; shift 2; continue 74 ;; 75 76 -i|--i|-ins|--ins|-insensitive|--insensitive) ins=1; shift; continue ;; 77 78 -tsv|--tsv) tsv=1; shift; continue ;; 79 80 -) break ;; 81 82 --) shift; break ;; 83 84 -*) 85 printf "%s: unsupported option %s\n" "$0" "$1" >&2 86 exit 1 87 ;; 88 esac 89 90 break 91 done 92 93 expr="${1:-\$0}" 94 [ $# -gt 0 ] && shift 95 if [ "${expr}" = '.' ]; then 96 expr='$0' 97 fi 98 99 # show all non-existing files given 100 failed=0 101 for a in "$@"; do 102 [ "$a" = '-' ] && continue 103 [ -e "$a" ] && continue 104 printf "%s: no file named \"%s\"\n" "$0" "$a" >&2 105 failed=1 106 done 107 108 [ "${failed}" -gt 0 ] && exit 2 109 110 ci=' 111 BEGIN { 112 if (IGNORECASE == "") { 113 m = "your `awk` command lacks case-insensitive regex-matching" 114 print(m) > "/dev/stderr" 115 exit 125 116 } 117 IGNORECASE = 1 118 } 119 ' 120 if [ "${ins}" -eq 0 ]; then 121 ci='' 122 fi 123 124 flush='' 125 if [ "${buf}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then 126 flush='; fflush()' 127 fi 128 129 src="${ci}"' 130 { print ('"${expr}"')'"${flush}"' } 131 ' 132 133 if [ "${tsv}" -eq 1 ]; then 134 ${cmd} -F "\t" -v OFS="\t" "${src}" "$@" 135 else 136 ${cmd} "${src}" "$@" 137 fi