File: dcol.sh
   1 #!/bin/sh
   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 # dcol [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 
  44 case "$1" in
  45     -h|--h|-help|--help)
  46         awk '/^# +dcol /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  47         exit 0
  48     ;;
  49 esac
  50 
  51 [ "$1" = "--" ] && shift
  52 
  53 command='awk'
  54 if [ -p /dev/stdout ] || [ -t /dev/stdout ]; then
  55     command='stdbuf -oL awk'
  56 fi
  57 
  58 ${command} -F "\t" '
  59     function findcol(name, lowname, i) {
  60         for (i = 1; i <= NF; i++) if (name == $i) return i
  61         for (i = 1; i <= NF; i++) if (lowname == tolower($i)) return i
  62 
  63         if (1 <= name && name <= NF) return name + 0
  64         if (name < 0 && -name <= NF) return NF + name + 1
  65 
  66         return 0
  67     }
  68 
  69     BEGIN {
  70         for (i = 1; i < ARGC; i++) {
  71             colnames[++n] = ARGV[i]
  72             lownames[n] = tolower(ARGV[i])
  73             delete ARGV[i]
  74         }
  75     }
  76 
  77     { gsub(/\r$/, "") }
  78 
  79     FNR == 1 { FS = ($0 ~ /\t/) ? "\t" : " "; $0 = $0 }
  80 
  81     NR == 1 {
  82         numcols = NF
  83         for (i = 1; i <= numcols; i++) keep[i] = 1
  84 
  85         colsused = numcols
  86         for (i = 1; i <= n; i++) {
  87             j = findcol(colnames[i], lownames[i])
  88             if (j > 0) {
  89                 keep[j] = 0
  90                 colsused--
  91             }
  92 
  93             if (j == 0) {
  94                 fmt = "\x1b[31mno column match for \"%s\"\x1b[0m\n"
  95                 printf(fmt, colnames[i]) > "/dev/stderr"
  96                 errors++
  97             }
  98         }
  99 
 100         if (errors > 0) exit 1
 101         if (colsused == 0) exit
 102     }
 103 
 104     {
 105         c = 0
 106         for (i = 1; i <= numcols; i++) {
 107             if (keep[i] == 1) {
 108                 if (c > 0) printf "\t"
 109                 printf "%s", $i
 110                 c++
 111             }
 112         }
 113 
 114         printf "\n"
 115     }
 116 ' "$@"