File: rtsv.awk
   1 #!/usr/bin/awk -f
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2024 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 function match_number(v) {
  33     return match(v, /^[+-]?[0-9]+(\.[0-9]+)?$/)
  34 }
  35 
  36 function match_dot_digits(v) {
  37     return match(v, /\.[0-9]+$/)
  38 }
  39 
  40 BEGIN { FS = "\t" }
  41 
  42 {
  43     gsub(/\r$/, "")
  44 
  45     for (i = 1; i <= NF; i++) {
  46         data[NR][i] = $i
  47 
  48         if (match_number($i)) {
  49             if (match_dot_digits($i)) {
  50                 dd = RLENGTH
  51                 if (dot_decs[i] < dd) dot_decs[i] = dd
  52                 iw = RSTART - 1
  53                 if (int_widths[i] < iw) int_widths[i] = iw
  54             } else {
  55                 w = length($i)
  56                 if (int_widths[i] < w) int_widths[i] = w
  57             }
  58 
  59             continue
  60         }
  61 
  62         w = length($i)
  63         if (widths[i] < w) widths[i] = w
  64     }
  65 }
  66 
  67 END {
  68     # fix column-widths using the number-padding info
  69     for (i = 1; i <= NF; i++) {
  70         w = int_widths[i] + dot_decs[i]
  71         if (widths[i] < w) widths[i] = w
  72     }
  73 
  74     for (i = 1; i <= NR; i++) {
  75         last = length(data[i])
  76 
  77         for (j = 1; j <= last; j++) {
  78             if (j > 1) printf "  " # put 2-space gaps between columns
  79 
  80             v = data[i][j]
  81 
  82             if (!match_number(v)) {
  83                 # avoid adding trailing spaces at the end of lines
  84                 printf "%*s", (j == last) ? 0 : -widths[j], v
  85                 continue
  86             }
  87 
  88             w = length(v)
  89             if (match_dot_digits(v)) {
  90                 dd = RLENGTH
  91                 iw = RSTART - 1
  92             } else {
  93                 dd = 0
  94                 iw = w
  95             }
  96 
  97             dpad = dot_decs[j] - dd
  98             ipad = int_widths[j] - iw
  99             if (ipad < 0) ipad = 0
 100             lpad = widths[j] - (ipad + w + dpad)
 101             if (lpad < 0) lpad = 0
 102 
 103             # avoid adding trailing spaces at the end of lines
 104             if (j == last) dpad = 0
 105 
 106             printf "%*s%*s%s%*s", lpad, "", ipad, "", v, dpad, ""
 107         }
 108 
 109         printf "\n"
 110     }
 111 }