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