File: backsort.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 # backsort [names/indices...]
  27 #
  28 # BACKward-SORT (numerically) using values from the columns whose names match
  29 # the arguments given, either exactly, case-insensitively, as 1-based indices,
  30 # or even as negative/backward indices.
  31 #
  32 # Sorting happens by comparing fields in the order given, stopping comparisons
  33 # as soon as (ordering) ties are broken.
  34 #
  35 # Input always comes from the standard input. The output is always lines of
  36 # TSV (tab-separated values), even when the input lines from stdin aren't.
  37 
  38 
  39 case "$1" in
  40     -h|--h|-help|--help)
  41         awk '/^# +backsort /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  42         exit 0
  43     ;;
  44 esac
  45 
  46 header=1
  47 case "$1" in
  48     -no-header|--no-header)
  49         header=0
  50         shift
  51     ;;
  52 esac
  53 
  54 [ "$1" = '--' ] && shift
  55 
  56 awk -v header="${header}" '
  57     function findcol(name, lowname, i) {
  58         for (i = 1; i <= NF; i++) {
  59             if (name == $i) return i
  60         }
  61 
  62         for (i = 1; i <= NF; i++) {
  63             if (lowname == tolower($i)) return i
  64         }
  65 
  66         if (1 <= name && name <= NF) return name + 0
  67         if (name < 0 && -name <= NF) return NF + name + 1
  68 
  69         return 0
  70     }
  71 
  72     BEGIN {
  73         for (i = 1; i < ARGC; i++) {
  74             colnames[i] = ARGV[i]
  75             lownames[i] = tolower(colnames[i])
  76             delete ARGV[i]
  77         }
  78     }
  79 
  80     { gsub(/\r$/, "") }
  81 
  82     NR == 1 {
  83         if ($0 ~ /\t/) {
  84             FS = "\t"
  85             $0 = $0
  86         }
  87 
  88         width = NF
  89         given = length(colnames)
  90 
  91         for (i = 1; i <= given; i++) {
  92             j = findcol(colnames[i], lownames[i])
  93             if (j > 0) pos[++numcols] = j
  94 
  95             if (j == 0) {
  96                 fmt = "no column match for \"%s\"\n"
  97                 printf(fmt, colnames[i]) > "/dev/stderr"
  98                 errors++
  99             }
 100         }
 101 
 102         if (errors > 0) exit 1
 103 
 104         cmd = "sort -s -t \"\t\""
 105         for (i = 1; i <= numcols; i++) {
 106             cmd = cmd sprintf(" -rnk%d,%d", pos[i], pos[i])
 107         }
 108 
 109         if (header) {
 110             for (i = 1; i <= width; i++) {
 111                 if (i > 1) printf "\t"
 112                 printf("%s", $i)
 113             }
 114             printf "\n"; fflush()
 115 
 116             next
 117         }
 118     }
 119 
 120     {
 121         for (i = 1; i <= width && i <= NF; i++) {
 122             if (i > 1) printf "\t" | cmd
 123             printf("%s", $i) | cmd
 124         }
 125         # fill-in missing trailing TSV fields
 126         for (i = NF + 1; i <= width; i++) printf "\t" | cmd
 127         # treat extra columns as part of the last one
 128         for (j = width + 1; j <= NF; j++) printf(" %s", $i) | cmd
 129         printf "\n" | cmd
 130     }
 131 ' "$@"