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...] [filenames...]
  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 case_insensitive=0
  67 
  68 while [ $# -gt 0 ]; do
  69     arg="$1"
  70 
  71     if [ "${arg}" = "--" ]; then
  72         shift
  73         break
  74     fi
  75 
  76     case "${arg}" in
  77         -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} ${arg}"
  90             shift
  91             continue
  92         ;;
  93 
  94         -v)
  95             shift
  96             if [ $# -eq 0 ]; then
  97                 printf "expected variable assignment after -v option\n" >&2
  98                 exit 1
  99             fi
 100             command="${command} -v $1"
 101             shift
 102             continue
 103         ;;
 104 
 105         -ins|--ins|-insensitive|--insensitive)
 106             case_insensitive=1
 107             shift
 108             continue
 109         ;;
 110 
 111         -tsv|--tsv)
 112             command="${command} -F \"\\t\""
 113             shift
 114             continue
 115         ;;
 116 
 117         -*)
 118             command="${command} ${arg}"
 119             shift
 120             continue
 121         ;;
 122     esac
 123 
 124     break
 125 done
 126 
 127 code="${1:-\$0}"
 128 [ $# -gt 0 ] && shift
 129 
 130 # show all non-existing files given
 131 failed=0
 132 for arg in "$@"; do
 133     if [ "${arg}" = "-" ]; then
 134         continue
 135     fi
 136     if [ ! -e "${arg}" ]; then
 137         printf "no file named \"%s\"\n" "${arg}" > /dev/stderr
 138         failed=1
 139     fi
 140 done
 141 
 142 if [ "${failed}" -gt 0 ]; then
 143     exit 2
 144 fi
 145 
 146 ci='
 147     BEGIN {
 148         if (IGNORECASE == "") {
 149             m = "your `awk` command lacks case-insensitive regex-matching"
 150             printf("\x1b[38;2;204;0;0m%s\x1b[0m\n", m) > "/dev/stderr"
 151             exit 125
 152         }
 153         IGNORECASE = 1
 154     }
 155 '
 156 if [ "${case_insensitive}" -eq 0 ]; then
 157     ci=''
 158 fi
 159 
 160 ${command} "${ci}"'
 161     BEGIN { for (i = 1; i < ARGC; i++) if (files[ARGV[i]]++) delete ARGV[i] }
 162     FNR == 1 { FS = /\t/ ? "\t" : " "; $0 = $0 }
 163     !c['"${code}"']++
 164 ' "$@"