File: rtsv.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 # rtsv [options...] [filenames...] 27 # 28 # Realign Tab Separated-Values, by padding with spaces to match each column's 29 # widest value, right-aligning all numbers. 30 31 32 case "$1" in 33 -h|--h|-help|--help) 34 awk '/^# +rtsv /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 35 exit 0 36 ;; 37 esac 38 39 40 awk -F "\t" ' 41 function match_number(v) { 42 return match(v, /^[+-]?[0-9]+(\.[0-9]+)?$/) 43 } 44 45 function match_dot_digits(v) { 46 return match(v, /\.[0-9]+$/) 47 } 48 49 { 50 gsub(/\r$/, "") 51 52 for (i = 1; i <= NF; i++) { 53 data[NR][i] = $i 54 55 if (match_number($i)) { 56 if (match_dot_digits($i)) { 57 dd = RLENGTH 58 if (dot_decs[i] < dd) dot_decs[i] = dd 59 iw = RSTART - 1 60 if (int_widths[i] < iw) int_widths[i] = iw 61 } else { 62 w = length($i) 63 if (int_widths[i] < w) int_widths[i] = w 64 } 65 66 continue 67 } 68 69 w = length($i) 70 if (widths[i] < w) widths[i] = w 71 } 72 } 73 74 END { 75 # fix column-widths using the number-padding info 76 for (i = 1; i <= NF; i++) { 77 w = int_widths[i] + dot_decs[i] 78 if (widths[i] < w) widths[i] = w 79 } 80 81 for (i = 1; i <= NR; i++) { 82 last = length(data[i]) 83 84 for (j = 1; j <= last; j++) { 85 if (j > 1) printf " " # put 2-space gaps between columns 86 87 v = data[i][j] 88 89 if (!match_number(v)) { 90 # avoid adding trailing spaces at the end of lines 91 printf "%*s", (j == last) ? 0 : -widths[j], v 92 continue 93 } 94 95 w = length(v) 96 if (match_dot_digits(v)) { 97 dd = RLENGTH 98 iw = RSTART - 1 99 } else { 100 dd = 0 101 iw = w 102 } 103 104 dpad = dot_decs[j] - dd 105 ipad = int_widths[j] - iw 106 if (ipad < 0) ipad = 0 107 lpad = widths[j] - (ipad + w + dpad) 108 if (lpad < 0) lpad = 0 109 110 # avoid adding trailing spaces at the end of lines 111 if (j == last) dpad = 0 112 113 printf "%*s%*s%s%*s", lpad, "", ipad, "", v, dpad, "" 114 } 115 116 printf "\n" 117 } 118 } 119 ' "$@"