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, -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 case "$1" in
  53     -h|--h|-help|--help)
  54         awk '/^# +uawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  55         exit 0
  56     ;;
  57 esac
  58 
  59 tsv=0
  60 buffered=0
  61 case_insensitive=0
  62 command='awk'
  63 
  64 while [ $# -gt 0 ]; do
  65     if [ "$1" = "--" ]; then
  66         shift
  67         break
  68     fi
  69 
  70     case "$1" in
  71         -b|--b|-buffered|--buffered)
  72             buffered=1
  73             shift
  74             continue
  75         ;;
  76 
  77         -f|-F)
  78             shift
  79             if [ $# -eq 0 ]; then
  80                 printf "expected value after -F option\n" >&2
  81                 exit 1
  82             fi
  83             command="${command} -F $1"
  84             shift
  85             continue
  86         ;;
  87 
  88         -F*)
  89             command="${command} $1"
  90             shift
  91             continue
  92         ;;
  93 
  94         -i|--i|-ins|--ins|-insensitive|--insensitive)
  95             case_insensitive=1
  96             shift
  97             continue
  98         ;;
  99 
 100         -tsv|--tsv)
 101             tsv=1
 102             shift
 103             continue
 104         ;;
 105     esac
 106 
 107     break
 108 done
 109 
 110 code="${1:-\$0}"
 111 [ $# -gt 0 ] && shift
 112 
 113 # show all non-existing files given
 114 failed=0
 115 for arg in "$@"; do
 116     if [ "${arg}" = "-" ]; then
 117         continue
 118     fi
 119     if [ ! -e "${arg}" ]; then
 120         printf "no file named \"%s\"\n" "${arg}" >&2
 121         failed=1
 122     fi
 123 done
 124 
 125 if [ "${failed}" -gt 0 ]; then
 126     exit 2
 127 fi
 128 
 129 flush=''
 130 if [ "${buffered}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then
 131     flush='fflush()'
 132 fi
 133 
 134 ci='
 135     BEGIN {
 136         if (IGNORECASE == "") {
 137             m = "your `awk` command lacks case-insensitive regex-matching"
 138             print(m) > "/dev/stderr"
 139             exit 125
 140         }
 141         IGNORECASE = 1
 142     }
 143 '
 144 if [ "${case_insensitive}" -eq 0 ]; then
 145     ci=''
 146 fi
 147 
 148 src="${ci}"'
 149     BEGIN { for (i = 1; i < ARGC; i++) if (files[ARGV[i]]++) delete ARGV[i] }
 150     !c['"${code}"']++ { print; '"${flush}"' }
 151 '
 152 
 153 if [ "${tsv}" -eq 1 ]; then
 154     ${command} -F "\t" "${src}" "$@"
 155 else
 156     ${command} "${src}" "$@"
 157 fi