File: regsort.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 # regsort [option...] [regex...] [files...]
  27 #
  28 # Regular Expression Group SORT groups/sorts lines according to common results
  29 # of matching the extended-mode regular-expression given. Each match group
  30 # starts appearing in the output depending on the order the first line matching
  31 # it appeared in the input.
  32 #
  33 # The options are, available both in single and double-dash versions
  34 #
  35 #   -i, -ins, -insensitive    match the regex given case-insensitively
  36 #   -h, -help                 show this help message
  37 
  38 
  39 case "$1" in
  40     -h|--h|-help|--help)
  41         awk '/^# +regsort /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  42         exit 0
  43     ;;
  44 esac
  45 
  46 case_insensitive=0
  47 case "$1" in
  48     -i|--i|-ins|--ins|-insensitive|--insensitive)
  49         case_insensitive=1
  50         shift
  51     ;;
  52 esac
  53 
  54 [ "$1" = '--' ] && shift
  55 
  56 if [ $# -eq 0 ]; then
  57     awk '/^# +regsort /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2
  58     exit 1
  59 fi
  60 regex="$1"
  61 shift
  62 
  63 # show all non-existing files given
  64 failed=0
  65 for arg in "$@"; do
  66     if [ "${arg}" = "-" ]; then
  67         continue
  68     fi
  69     if [ ! -e "${arg}" ]; then
  70         printf "no file named \"%s\"\n" "${arg}" >&2
  71         failed=1
  72     fi
  73 done
  74 
  75 if [ "${failed}" -gt 0 ]; then
  76     exit 2
  77 fi
  78 
  79 flush=0
  80 if [ -p /dev/stdout ] || [ -t 1 ]; then
  81     flush=1
  82 fi
  83 
  84 awk -v ci="${case_insensitive}" -v regex="${regex}" -v flush="${flush}" '
  85     BEGIN {
  86         if (SUBSEP == "") SUBSEP = "\034"
  87 
  88         if (ci == 1 && IGNORECASE == "") {
  89             m = "your `awk` command lacks case-insensitive regex-matching"
  90             print(m) > "/dev/stderr"
  91             exit 125
  92         }
  93         if (ci == 1) IGNORECASE = 1
  94     }
  95 
  96     {
  97         k = match($0, regex) ? substr($0, RSTART, RLENGTH) : ""
  98 
  99         if (numkeys == 0) {
 100             first = k
 101             numkeys++
 102         }
 103 
 104         # the first group is the only one which can be shown right away
 105         if (numkeys > 0 && k == first) {
 106             print
 107             if (flush) fflush()
 108             next
 109         }
 110 
 111         if (tally[k]++ == 0) ordkeys[++numkeys] = k
 112         groups[k SUBSEP tally[k]] = $0
 113     }
 114 
 115     END {
 116         for (i = 2; i <= numkeys; i++) {
 117             k = ordkeys[i]
 118             n = tally[k]
 119             for (j = 1; j <= n; j++) print groups[k SUBSEP j]
 120         }
 121     }
 122 ' "$@"