File: uawk.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 # uawk [options...] [awk expression...] [files...] 27 # 28 # 29 # Unique via AWK avoids lines duplicating the AWK expression given, by 30 # emitting only the first line which gives the expression each of its 31 # unique results. 32 # 33 # When not given any expression, whole lines are used: this effectively 34 # makes it a line deduplicator which keeps lines in their original order, 35 # unlike `uniq`, which requires sorted lines to avoid repetitions. 36 # 37 # The handy case-insensitive shortcut options may cause this tool to fail, 38 # if the main AWK tool installed doesn't support the special IGNORECASE 39 # variable. 40 # 41 # The AWK options available only in single-dash versions are 42 # 43 # -F fs use `fs` for the field separator (the `FS` variable) 44 # -V show the AWK version installed 45 # -v var=val set a variable to a given value 46 # 47 # The other options are, available both in single and double-dash versions 48 # 49 # -h, -help show this help message 50 # -ins, -insensitive match regexes case-insensitively; fail if unsupported 51 # -tsv split fields using tabs, same as using -F "\t" 52 53 54 case "$1" in 55 -h|--h|-help|--help) 56 awk '/^# +uawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 57 exit 0 58 ;; 59 esac 60 61 command='awk' 62 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 63 command='stdbuf -oL awk' 64 fi 65 66 tsv=0 67 case_insensitive=0 68 69 while [ $# -gt 0 ]; do 70 arg="$1" 71 72 if [ "${arg}" = "--" ]; then 73 shift 74 break 75 fi 76 77 case "${arg}" in 78 -F) 79 shift 80 if [ $# -eq 0 ]; then 81 printf "expected value after -F option\n" >&2 82 exit 1 83 fi 84 command="${command} -F $1" 85 shift 86 continue 87 ;; 88 89 -F*) 90 command="${command} ${arg}" 91 shift 92 continue 93 ;; 94 95 -v) 96 shift 97 if [ $# -eq 0 ]; then 98 printf "expected variable assignment after -v option\n" >&2 99 exit 1 100 fi 101 command="${command} -v $1" 102 shift 103 continue 104 ;; 105 106 -ins|--ins|-insensitive|--insensitive) 107 case_insensitive=1 108 shift 109 continue 110 ;; 111 112 -tsv|--tsv) 113 tsv=1 114 shift 115 continue 116 ;; 117 118 -*) 119 command="${command} ${arg}" 120 shift 121 continue 122 ;; 123 esac 124 125 break 126 done 127 128 code="${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}" > /dev/stderr 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_insensitive}" -eq 0 ]; then 158 ci='' 159 fi 160 161 src="${ci}"' 162 BEGIN { for (i = 1; i < ARGC; i++) if (files[ARGV[i]]++) delete ARGV[i] } 163 FNR == 1 { FS = /\t/ ? "\t" : " "; $0 = $0 } 164 !c['"${code}"']++ 165 ' 166 167 if [ "${tsv}" -eq 1 ]; then 168 ${command} -F "\t" "${src}" "$@" 169 else 170 ${command} "${src}" "$@" 171 fi