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