File: backsort.awk
   1 #!/usr/bin/awk -f
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2024 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 column whose name matches
  29 # the argument given, either exactly, case-insensitively, as a 1-based index,
  30 # or even as a negative/backward index.
  31 #
  32 # The output is always lines of TSV (tab-separated values) items, even when
  33 # the lines from stdin aren't.
  34 
  35 
  36 function findcol(name, lowname, i) {
  37     for (i = 1; i <= NF; i++) {
  38         if (name == $i) return i
  39     }
  40 
  41     for (i = 1; i <= NF; i++) {
  42         if (lowname == tolower($i)) return i
  43     }
  44 
  45     if (1 <= name && name <= NF) return name + 0
  46     if (name < 0 && -name <= NF) return NF + name + 1
  47 
  48     return 0
  49 }
  50 
  51 BEGIN {
  52     for (i = 1; i < ARGC; i++) {
  53         colnames[i] = ARGV[i]
  54         lownames[i] = tolower(colnames[i])
  55         delete ARGV[i]
  56     }
  57 }
  58 
  59 { gsub(/\r$/, "") }
  60 
  61 NR == 1 {
  62     if ($0 ~ /\t/) {
  63         FS = "\t"
  64         $0 = $0
  65     }
  66 
  67     width = NF
  68     for (i = 1; i <= width; i++) {
  69         j = findcol(colnames[i], lownames[i])
  70         if (j > 0) pos[++numcols] = j
  71     }
  72 
  73     if (numcols == 0) exit
  74 
  75     cmd = "sort -t '\t'"
  76     for (i = 1; i <= numcols; i++) {
  77         cmd = cmd sprintf(" -rnk%d", pos[i])
  78     }
  79 
  80     for (i = 1; i <= width; i++) {
  81         if (i > 1) printf "\t"
  82         printf("%s", $i)
  83     }
  84     printf "\n"; fflush()
  85 
  86     next
  87 }
  88 
  89 {
  90     for (i = 1; i <= width; i++) {
  91         if (i > 1) printf "\t" | cmd
  92         printf("%s", $i) | cmd
  93     }
  94     printf "\n" | cmd
  95 }