File: pcol.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 2026 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 # pcol [options...] [column names...]
  27 #
  28 #
  29 # Pick COLumns lets you select/reorder a subset of a table's columns, matching
  30 # the column names given using the first line from the standard input. Input
  31 # lines can be either space-separated or tab-separated; output lines are always
  32 # TSV (Tab-Separated Values) ones, where trailing tabs are added if any values
  33 # are missing.
  34 #
  35 # When a column name isn't matched exactly, a case-insensitive match is tried:
  36 # if the latter also fails, number-matching is finally tried, before giving up
  37 # on that column name. Column numbers start at 1, and can be negative to count
  38 # backward from the last column.
  39 #
  40 # The options are, available both in single and double-dash versions
  41 #
  42 #   -h, -help    show this help message
  43 
  44 
  45 case "$1" in
  46     -h|--h|-help|--help)
  47         awk '/^# +pcol /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  48         exit 0
  49     ;;
  50 
  51     --) shift ;;
  52 esac
  53 
  54 flush=0
  55 if [ -p /dev/stdout ] || [ -t 1 ]; then
  56     flush=1
  57 fi
  58 
  59 awk -v flush="${flush}" '
  60     function findcol(name, lowname, i) {
  61         for (i = 1; i <= NF; i++) if (name == $i) return i
  62         for (i = 1; i <= NF; i++) if (lowname == tolower($i)) return i
  63 
  64         if (1 <= name && name <= NF) return name + 0
  65         if (name < 0 && -name <= NF) return NF + name + 1
  66 
  67         return 0
  68     }
  69 
  70     BEGIN {
  71         for (i = 1; i < ARGC; i++) {
  72             colnames[++n] = ARGV[i]
  73             lownames[n] = tolower(ARGV[i])
  74             delete ARGV[i]
  75         }
  76     }
  77 
  78     { gsub(/\r$/, "") }
  79 
  80     FNR == 1 { FS = /\t/ ? "\t" : " "; $0 = $0 }
  81 
  82     NR == 1 {
  83         for (i = 1; i <= n; i++) {
  84             j = findcol(colnames[i], lownames[i])
  85             if (j > 0) pick[++ncols] = j
  86 
  87             if (j == 0) {
  88                 fmt = "no column match for \"%s\"\n"
  89                 printf(fmt, colnames[i]) > "/dev/stderr"
  90                 errors++
  91             }
  92         }
  93 
  94         if (errors > 0) exit 1
  95         if (ncols == 0) exit
  96     }
  97 
  98     {
  99         for (i = 1; i <= ncols; i++) {
 100             if (i > 1) printf "\t"
 101             printf "%s", $(pick[i])
 102         }
 103         print ""
 104         if (flush) fflush()
 105     }
 106 ' "$@"