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