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