File: grawk.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 # grawk [options...] [awk expression...] [files...]
  27 #
  28 #
  29 # GRoup via AWK groups lines using common results of the AWK expression given.
  30 # When not given any, whole lines are used, putting identical lines next to
  31 # each other.
  32 #
  33 # The handy case-insensitive shortcut options may cause this tool to fail,
  34 # if the main AWK tool installed doesn't support the special IGNORECASE
  35 # variable.
  36 #
  37 # The AWK options available only in single-dash versions are
  38 #
  39 #   -f fs, -F fs, -Ffs, -F=fs    make `fs` the field separator
  40 #
  41 # The other options are, available both in single and double-dash versions
  42 #
  43 #   -h, -help    show this help message
  44 #   -i, -ins     match regexes case-insensitively; may fail the default `awk`
  45 #   -tsv         split fields using tabs, same as using -F "\t"
  46 
  47 
  48 case "$1" in
  49     -h|--h|-help|--help)
  50         awk '/^# +grawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  51         exit 0
  52     ;;
  53 esac
  54 
  55 tsv=0
  56 ins=0
  57 buffered=0
  58 command='awk'
  59 
  60 while [ $# -gt 0 ]; do
  61     if [ "$1" = "--" ]; then
  62         shift
  63         break
  64     fi
  65 
  66     case "$1" in
  67         -b|--b|-buffered|--buffered)
  68             buffered=1
  69             shift
  70             continue
  71         ;;
  72 
  73         -f|-F)
  74             shift
  75             if [ $# -eq 0 ]; then
  76                 printf "expected value after -F option\n" >&2
  77                 exit 1
  78             fi
  79             command="${command} -F $1"
  80             shift
  81             continue
  82         ;;
  83 
  84         -F*)
  85             command="${command} $1"
  86             shift
  87             continue
  88         ;;
  89 
  90         -i|--i|-ins|--ins|-insensitive|--insensitive)
  91             ins=1
  92             shift
  93             continue
  94         ;;
  95 
  96         -tsv|--tsv)
  97             tsv=1
  98             shift
  99             continue
 100         ;;
 101     esac
 102 
 103     break
 104 done
 105 
 106 code="${1:-\$0}"
 107 [ $# -gt 0 ] && shift
 108 
 109 # show all non-existing files given
 110 failed=0
 111 for arg in "$@"; do
 112     if [ "${arg}" = "-" ]; then
 113         continue
 114     fi
 115     if [ ! -e "${arg}" ]; then
 116         printf "no file named \"%s\"\n" "${arg}" >&2
 117         failed=1
 118     fi
 119 done
 120 
 121 if [ "${failed}" -gt 0 ]; then
 122     exit 2
 123 fi
 124 
 125 flush=0
 126 if [ "${buffered}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then
 127     flush=1
 128 fi
 129 
 130 ci='
 131     BEGIN {
 132         if (IGNORECASE == "") {
 133             m = "your `awk` command lacks case-insensitive regex-matching"
 134             print(m) > "/dev/stderr"
 135             exit 125
 136         }
 137         IGNORECASE = 1
 138     }
 139 '
 140 if [ "${ins}" -eq 0 ]; then
 141     ci=''
 142 fi
 143 
 144 src="${ci}"'
 145     BEGIN { if (SUBSEP == "") SUBSEP = "\034" }
 146 
 147     {
 148         k = ('"${code}"')
 149 
 150         if (numkeys == 0) {
 151             first = k
 152             numkeys++
 153         }
 154 
 155         # the first group is the only one which can be shown right away
 156         if (numkeys > 0 && k == first) {
 157             print
 158             if (flush) fflush()
 159             next
 160         }
 161 
 162         if (tally[k]++ == 0) ordkeys[++numkeys] = k
 163         groups[k SUBSEP tally[k]] = $0
 164     }
 165 
 166     END {
 167         for (i = 2; i <= numkeys; i++) {
 168             k = ordkeys[i]
 169             n = tally[k]
 170             for (j = 1; j <= n; j++) print groups[k SUBSEP j]
 171         }
 172     }
 173 '
 174 
 175 if [ "${tsv}" -eq 1 ]; then
 176     ${command} -F "\t" -v flush="${flush}" "${src}" "$@"
 177 else
 178     ${command} -v flush="${flush}" "${src}" "$@"
 179 fi