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