File: msort.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 # msort [option...] [regex...] [files...]
  27 #
  28 # Match SORT groups/sorts lines according to common results of matching the
  29 # extended-mode regular-expression given. Each match group starts appearing
  30 # in the output depending on the order the first line matching it appeared
  31 # 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 '/^# +msort /, /^$/ { 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 '/^# +msort /, /^$/ { 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}" > /dev/stderr
  71         failed=1
  72     fi
  73 done
  74 
  75 if [ "${failed}" -gt 0 ]; then
  76     exit 2
  77 fi
  78 
  79 awk -v ci="${case_insensitive}" -v regex="${regex}" '
  80     BEGIN {
  81         if (SUBSEP == "") SUBSEP = "\034"
  82 
  83         if (ci == 1 && IGNORECASE == "") {
  84             m = "your `awk` command lacks case-insensitive regex-matching"
  85             print(m) > "/dev/stderr"
  86             exit 125
  87         }
  88         if (ci == 1) IGNORECASE = 1
  89     }
  90 
  91     {
  92         k = match($0, regex) ? substr($0, RSTART, RLENGTH) : ""
  93         if (tally[k]++ == 0) ordkeys[++numkeys] = k
  94         groups[k SUBSEP tally[k]] = $0
  95     }
  96 
  97     END {
  98         for (i = 1; i <= numkeys; i++) {
  99             k = ordkeys[i]
 100             n = tally[k]
 101             for (j = 1; j <= n; j++) print groups[k SUBSEP j]
 102         }
 103     }
 104 ' "$@"