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