File: msort.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2025 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...] [filenames...]
  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              match the regex given case-insensitively
  36 #   -ins            match the regex given case-insensitively
  37 #   -insensitive    match the regex given case-insensitively
  38 #
  39 #   -h              show this help message
  40 #   -help           show this help message
  41 
  42 
  43 case "$1" in
  44     -h|--h|-help|--help)
  45         awk '/^# +msort /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  46         exit 0
  47     ;;
  48 esac
  49 
  50 case_insensitive=0
  51 case "$1" in
  52     -i|--i|-ins|--ins|-insensitive|--insensitive)
  53         case_insensitive=1
  54         shift
  55     ;;
  56 esac
  57 
  58 [ "$1" = "--" ] && shift
  59 
  60 if [ $# -eq 0 ]; then
  61     awk '/^# +msort /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2
  62     exit 1
  63 fi
  64 regex="$1"
  65 shift
  66 
  67 command='awk'
  68 if [ -e /usr/bin/gawk ]; then
  69     command='gawk'
  70 fi
  71 
  72 # show all non-existing files given
  73 failed=0
  74 for arg in "$@"; do
  75     if [ "${arg}" = "-" ]; then
  76         continue
  77     fi
  78     if [ ! -e "${arg}" ]; then
  79         printf "no file named \"%s\"\n" "${arg}" > /dev/stderr
  80         failed=1
  81     fi
  82 done
  83 
  84 if [ "${failed}" -gt 0 ]; then
  85     exit 2
  86 fi
  87 
  88 ${command} -v ci="${case_insensitive}" -v regex="${regex}" '
  89     BEGIN {
  90         if (ci == 1 && IGNORECASE == "") {
  91             m = "your `awk` command lacks case-insensitive regex-matching"
  92             printf("\x1b[38;2;204;0;0m%s\x1b[0m\n", m) > "/dev/stderr"
  93             exit 125
  94         }
  95         if (ci == 1) IGNORECASE = 1
  96     }
  97 
  98     {
  99         k = match($0, regex) ? substr($0, RSTART, RLENGTH) : ""
 100         if (!(k in groups)) ordkeys[++oklen] = k
 101         groups[k][length(groups[k]) + 1] = $0
 102     }
 103 
 104     END {
 105         for (i = 1; i <= oklen; i++) {
 106             k = ordkeys[i]
 107             n = length(groups[k])
 108             for (j = 1; j <= n; j++) print groups[k][j]
 109         }
 110     }
 111 ' "$@"