File: backsort.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2020-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 [ "$1" = "--" ] && shift
  46 
  47 awk '
  48     function findcol(name, lowname, i) {
  49         for (i = 1; i <= NF; i++) {
  50             if (name == $i) return i
  51         }
  52 
  53         for (i = 1; i <= NF; i++) {
  54             if (lowname == tolower($i)) return i
  55         }
  56 
  57         if (1 <= name && name <= NF) return name + 0
  58         if (name < 0 && -name <= NF) return NF + name + 1
  59 
  60         return 0
  61     }
  62 
  63     BEGIN {
  64         for (i = 1; i < ARGC; i++) {
  65             colnames[i] = ARGV[i]
  66             lownames[i] = tolower(colnames[i])
  67             delete ARGV[i]
  68         }
  69     }
  70 
  71     { gsub(/\r$/, "") }
  72 
  73     NR == 1 {
  74         if ($0 ~ /\t/) {
  75             FS = "\t"
  76             $0 = $0
  77         }
  78 
  79         width = NF
  80         given = length(colnames)
  81 
  82         for (i = 1; i <= given; i++) {
  83             j = findcol(colnames[i], lownames[i])
  84             if (j > 0) pos[++numcols] = j
  85 
  86             if (j == 0) {
  87                 fmt = "\x1b[31mno column match for \"%s\"\x1b[0m\n"
  88                 printf(fmt, colnames[i]) > "/dev/stderr"
  89                 errors++
  90             }
  91         }
  92 
  93         if (errors > 0) exit 1
  94 
  95         cmd = "sort -t \"\t\""
  96         for (i = 1; i <= numcols; i++) {
  97             cmd = cmd sprintf(" -rnk%d", pos[i])
  98         }
  99 
 100         for (i = 1; i <= width; i++) {
 101             if (i > 1) printf "\t"
 102             printf("%s", $i)
 103         }
 104         printf "\n"; fflush()
 105 
 106         next
 107     }
 108 
 109     {
 110         for (i = 1; i <= width; i++) {
 111             if (i > 1) printf "\t" | cmd
 112             printf("%s", $i) | cmd
 113         }
 114         printf "\n" | cmd
 115     }
 116 ' "$@"