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, -Ffs, -F=fs    make `fs` the field separator
  44 #
  45 # The other options are, available both in single and double-dash versions
  46 #
  47 #   -h, -help    show this help message
  48 #   -i, -ins     match regexes case-insensitively; may fail the default `awk`
  49 #   -tsv         split fields using tabs, same as using -F "\t"
  50 
  51 
  52 tsv=0
  53 buf=0
  54 ins=0
  55 cmd='awk'
  56 
  57 while [ $# -gt 0 ]; do
  58     case "$1" in
  59         -b|--b|-buffered|--buffered) buf=1; shift; continue ;;
  60 
  61         -F)
  62             if [ $# -lt 2 ]; then
  63                 printf "expected value after -F option\n" >&2
  64                 exit 1
  65             fi
  66             cmd="${cmd} -F $2"; shift 2; continue
  67         ;;
  68 
  69         -F*) cmd="${cmd} $1"; shift; continue ;;
  70 
  71         -h|--h|-help|--help)
  72             awk '/^# +uawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  73             exit 0
  74         ;;
  75 
  76         -i|--i|-ins|--ins|-insensitive|--insensitive)
  77             ins=1; shift; continue
  78         ;;
  79 
  80         -tsv|--tsv) tsv=1; shift; continue ;;
  81 
  82         -v)
  83             if [ $# -lt 2 ]; then
  84                 printf "expected variable assignment after -v option\n" >&2
  85                 exit 1
  86             fi
  87             cmd="${cmd} -v $2"; shift 2; continue
  88         ;;
  89 
  90         -) break ;;
  91 
  92         --) shift; break ;;
  93 
  94         -*)
  95             printf "unsupported option '%s'\n" "$1" >&2
  96             exit 1
  97         ;;
  98     esac
  99 
 100     break
 101 done
 102 
 103 code="${1:-\$0}"
 104 [ $# -gt 0 ] && shift
 105 
 106 # show all non-existing files given
 107 failed=0
 108 for arg in "$@"; do
 109     [ "${arg}" = "-" ] && continue
 110     [ -e "${arg}" ] && continue
 111     printf "no file named \"%s\"\n" "${arg}" >&2
 112     failed=1
 113 done
 114 
 115 [ "${failed}" -gt 0 ] && exit 2
 116 
 117 flush=''
 118 if [ "${buf}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then
 119     flush='fflush()'
 120 fi
 121 
 122 ci='
 123     BEGIN {
 124         if (IGNORECASE == "") {
 125             m = "your `awk` command lacks case-insensitive regex-matching"
 126             print(m) > "/dev/stderr"
 127             exit 125
 128         }
 129         IGNORECASE = 1
 130     }
 131 '
 132 if [ "${ins}" -eq 0 ]; then
 133     ci=''
 134 fi
 135 
 136 src="${ci}"'
 137     BEGIN { for (i = 1; i < ARGC; i++) if (files[ARGV[i]]++) delete ARGV[i] }
 138     !c['"${code}"']++ { print; '"${flush}"' }
 139 '
 140 
 141 if [ "${tsv}" -eq 1 ]; then
 142     ${cmd} -F "\t" "${src}" "$@"
 143 else
 144     ${cmd} "${src}" "$@"
 145 fi