#!/bin/sh # The MIT License (MIT) # # Copyright (c) 2026 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # backsort [names/indices...] # # BACKward-SORT (numerically) using values from the columns whose names match # the arguments given, either exactly, case-insensitively, as 1-based indices, # or even as negative/backward indices. # # Sorting happens by comparing fields in the order given, stopping comparisons # as soon as (ordering) ties are broken. # # Input always comes from the standard input. The output is always lines of # TSV (tab-separated values), even when the input lines from stdin aren't. case "$1" in -h|--h|-help|--help) awk '/^# +backsort /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac header=1 case "$1" in -no-header|--no-header) header=0 shift ;; esac [ "$1" = '--' ] && shift awk -v header="${header}" ' function findcol(name, lowname, i) { for (i = 1; i <= NF; i++) { if (name == $i) return i } for (i = 1; i <= NF; i++) { if (lowname == tolower($i)) return i } if (1 <= name && name <= NF) return name + 0 if (name < 0 && -name <= NF) return NF + name + 1 return 0 } BEGIN { for (i = 1; i < ARGC; i++) { colnames[i] = ARGV[i] lownames[i] = tolower(colnames[i]) delete ARGV[i] } } { gsub(/\r$/, "") } NR == 1 { if ($0 ~ /\t/) { FS = "\t" $0 = $0 } width = NF given = ARGC - 1 for (i = 1; i <= given; i++) { j = findcol(colnames[i], lownames[i]) if (j > 0) pos[++numcols] = j if (j == 0) { fmt = "no column match for \"%s\"\n" printf(fmt, colnames[i]) > "/dev/stderr" errors++ } } if (errors > 0) exit 1 cmd = "sort -s -t \"\t\"" for (i = 1; i <= numcols; i++) { cmd = cmd sprintf(" -rnk%d,%d", pos[i], pos[i]) } if (header) { for (i = 1; i <= width; i++) { if (i > 1) printf "\t" printf("%s", $i) } printf "\n"; fflush() next } } { for (i = 1; i <= width && i <= NF; i++) { if (i > 1) printf "\t" | cmd printf("%s", $i) | cmd } # fill-in missing trailing TSV fields for (i = NF + 1; i <= width; i++) printf "\t" | cmd # treat extra columns as part of the last one for (j = width + 1; j <= NF; j++) printf(" %s", $i) | cmd printf "\n" | cmd } ' "$@"