#!/bin/sh # The MIT License (MIT) # # Copyright (c) 2026 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # uawk [options...] [awk expression...] [filenames...] # # # Unique via AWK avoids lines duplicating the AWK expression given, by # emitting only the first line which gives the expression each of its # unique results. # # When not given any expression, whole lines are used: this effectively # makes it a line deduplicator which keeps lines in their original order, # unlike `uniq`, which requires sorted lines to avoid repetitions. # # The handy case-insensitive shortcut options may cause this tool to fail, # if the main AWK tool installed doesn't support the special IGNORECASE # variable. # # The AWK options available only in single-dash versions are # # -F fs use `fs` for the field separator (the `FS` variable) # -V show the AWK version installed # -v var=val set a variable to a given value # # The other options are, available both in single and double-dash versions # # -h, -help show this help message # -ins, -insensitive match regexes case-insensitively; fail if unsupported # -tsv split fields using tabs, same as using -F "\t" case "$1" in -h|--h|-help|--help) awk '/^# +uawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac command='awk' if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then command='stdbuf -oL awk' fi tsv=0 case_insensitive=0 while [ $# -gt 0 ]; do arg="$1" if [ "${arg}" = "--" ]; then shift break fi case "${arg}" in -F) shift if [ $# -eq 0 ]; then printf "expected value after -F option\n" >&2 exit 1 fi command="${command} -F $1" shift continue ;; -F*) command="${command} ${arg}" shift continue ;; -v) shift if [ $# -eq 0 ]; then printf "expected variable assignment after -v option\n" >&2 exit 1 fi command="${command} -v $1" shift continue ;; -ins|--ins|-insensitive|--insensitive) case_insensitive=1 shift continue ;; -tsv|--tsv) tsv=1 shift continue ;; -*) command="${command} ${arg}" shift continue ;; esac break done code="${1:-\$0}" [ $# -gt 0 ] && shift # show all non-existing files given failed=0 for arg in "$@"; do if [ "${arg}" = "-" ]; then continue fi if [ ! -e "${arg}" ]; then printf "no file named \"%s\"\n" "${arg}" > /dev/stderr failed=1 fi done if [ "${failed}" -gt 0 ]; then exit 2 fi ci=' BEGIN { if (IGNORECASE == "") { m = "your `awk` command lacks case-insensitive regex-matching" printf("\x1b[38;2;204;0;0m%s\x1b[0m\n", m) > "/dev/stderr" exit 125 } IGNORECASE = 1 } ' if [ "${case_insensitive}" -eq 0 ]; then ci='' fi src="${ci}"' BEGIN { for (i = 1; i < ARGC; i++) if (files[ARGV[i]]++) delete ARGV[i] } FNR == 1 { FS = /\t/ ? "\t" : " "; $0 = $0 } !c['"${code}"']++ ' if [ "${tsv}" -eq 1 ]; then ${command} -F "\t" "${src}" "$@" else ${command} "${src}" "$@" fi