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_insensitive=0
  40 
  41 case "$1" in
  42     -h|--h|-help|--help)
  43         awk '/^# +regsort /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  44         exit 0
  45     ;;
  46 
  47     -i|--i|-ins|--ins|-insensitive|--insensitive)
  48         case_insensitive=1
  49         shift
  50     ;;
  51 
  52     --) shift ;;
  53 
  54     -*)
  55         printf "unsupported option '%s'\n" "$1" >&2
  56         exit 1
  57     ;;
  58 esac
  59 
  60 if [ $# -eq 0 ]; then
  61     awk '/^# +regsort /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2
  62     exit 1
  63 fi
  64 regex="$1"
  65 shift
  66 
  67 # show all non-existing files given
  68 failed=0
  69 for arg in "$@"; do
  70     [ "${arg}" = "-" ] && continue
  71     [ -e "${arg}" ] && continue
  72     printf "no file named \"%s\"\n" "${arg}" >&2
  73     failed=1
  74 done
  75 
  76 [ "${failed}" -gt 0 ] && exit 2
  77 
  78 flush=0
  79 if [ -p /dev/stdout ] || [ -t 1 ]; then
  80     flush=1
  81 fi
  82 
  83 awk -v ci="${case_insensitive}" -v regex="${regex}" -v flush="${flush}" '
  84     BEGIN {
  85         if (SUBSEP == "") SUBSEP = "\034"
  86 
  87         if (ci == 1 && IGNORECASE == "") {
  88             m = "your `awk` command lacks case-insensitive regex-matching"
  89             print(m) > "/dev/stderr"
  90             exit 125
  91         }
  92         if (ci == 1) IGNORECASE = 1
  93     }
  94 
  95     {
  96         k = match($0, regex) ? substr($0, RSTART, RLENGTH) : ""
  97 
  98         if (n == 0) {
  99             first = k
 100             n++
 101         }
 102 
 103         # the first group is the only one which can be shown right away
 104         if (n > 0 && k == first) {
 105             print
 106             if (flush) fflush()
 107             next
 108         }
 109 
 110         if (tally[k]++ == 0) keys[++n] = k
 111         groups[k SUBSEP tally[k]] = $0
 112     }
 113 
 114     END {
 115         for (i = 2; i <= n; i++) {
 116             k = keys[i]
 117             n = tally[k]
 118             for (j = 1; j <= n; j++) print groups[k SUBSEP j]
 119         }
 120     }
 121 ' "$@"