File: dcol.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 # dcol [options...] [column names...]
  27 #
  28 #
  29 # Drop COLumns lets you ignore a subset of a table's columns, matching the
  30 # column names given using the first line from the standard input. Input lines
  31 # can be either space-separated or tab-separated; output lines are always TSV
  32 # (Tab-Separated Values) ones, where trailing tabs are added if any values are
  33 # 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 # Running this with no arguments is also useful, since no columns are dropped,
  41 # and you get TSV output with always the same number of fields per line.
  42 #
  43 # The options are, available both in single and double-dash versions
  44 #
  45 #   -h, -help    show this help message
  46 
  47 
  48 case "$1" in
  49     -h|--h|-help|--help)
  50         awk '/^# +dcol /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  51         exit 0
  52     ;;
  53 
  54     --) shift ;;
  55 esac
  56 
  57 flush=0
  58 if [ -p /dev/stdout ] || [ -t 1 ]; then
  59     flush=1
  60 fi
  61 
  62 awk -v flush="${flush}" '
  63     function findcol(name, lowname, i) {
  64         for (i = 1; i <= NF; i++) if (name == $i) return i
  65         for (i = 1; i <= NF; i++) if (lowname == tolower($i)) return i
  66 
  67         if (1 <= name && name <= NF) return name + 0
  68         if (name < 0 && -name <= NF) return NF + name + 1
  69 
  70         return 0
  71     }
  72 
  73     BEGIN {
  74         for (i = 1; i < ARGC; i++) {
  75             colnames[++n] = ARGV[i]
  76             lownames[n] = tolower(ARGV[i])
  77             delete ARGV[i]
  78         }
  79     }
  80 
  81     { gsub(/\r$/, "") }
  82 
  83     FNR == 1 { FS = /\t/ ? "\t" : " "; $0 = $0 }
  84 
  85     NR == 1 {
  86         ncols = NF
  87         for (i = 1; i <= ncols; i++) keep[i] = 1
  88 
  89         used = ncols
  90         for (i = 1; i <= n; i++) {
  91             j = findcol(colnames[i], lownames[i])
  92             if (j > 0) {
  93                 keep[j] = 0
  94                 used--
  95             }
  96 
  97             if (j == 0) {
  98                 fmt = "no column match for \"%s\"\n"
  99                 printf(fmt, colnames[i]) > "/dev/stderr"
 100                 errors++
 101             }
 102         }
 103 
 104         if (errors > 0) exit 1
 105         if (used == 0) exit
 106     }
 107 
 108     {
 109         c = 0
 110         for (i = 1; i <= ncols; i++) {
 111             if (keep[i] == 1) {
 112                 if (c > 0) printf "\t"
 113                 printf "%s", $i
 114                 c++
 115             }
 116         }
 117 
 118         print ""
 119         if (flush) fflush()
 120     }
 121 ' "$@"