File: realign.awk
   1 #!/usr/bin/awk -f
   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 # realign [filenames...]
  27 #
  28 # Realign all detected columns, right-aligning any detected numbers in any
  29 # column.
  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 { gsub(/\r$/, "") }
  41 
  42 FNR == 1 {
  43     FS = ($0 ~ /\t/) ? "\t" : " "
  44     $0 = $0
  45 }
  46 
  47 {
  48     for (i = 1; i <= NF; i++) {
  49         data[NR][i] = $i
  50 
  51         if (match_number($i)) {
  52             if (match_dot_digits($i)) {
  53                 dd = RLENGTH
  54                 if (dot_decs[i] < dd) dot_decs[i] = dd
  55                 iw = RSTART - 1
  56                 if (int_widths[i] < iw) int_widths[i] = iw
  57             } else {
  58                 w = length($i)
  59                 if (int_widths[i] < w) int_widths[i] = w
  60             }
  61 
  62             continue
  63         }
  64 
  65         w = length($i)
  66         if (widths[i] < w) widths[i] = w
  67     }
  68 }
  69 
  70 END {
  71     # fix column-widths using the number-padding info
  72     for (i = 1; i <= NF; i++) {
  73         w = int_widths[i] + dot_decs[i]
  74         if (widths[i] < w) widths[i] = w
  75     }
  76 
  77     for (i = 1; i <= NR; i++) {
  78         last = length(data[i])
  79 
  80         for (j = 1; j <= last; j++) {
  81             if (j > 1) printf "  " # put 2-space gaps between columns
  82 
  83             v = data[i][j]
  84 
  85             if (!match_number(v)) {
  86                 # avoid adding trailing spaces at the end of lines
  87                 printf "%*s", (j == last) ? 0 : -widths[j], v
  88                 continue
  89             }
  90 
  91             w = length(v)
  92             if (match_dot_digits(v)) {
  93                 dd = RLENGTH
  94                 iw = RSTART - 1
  95             } else {
  96                 dd = 0
  97                 iw = w
  98             }
  99 
 100             dpad = dot_decs[j] - dd
 101             ipad = int_widths[j] - iw
 102             if (ipad < 0) ipad = 0
 103             lpad = widths[j] - (ipad + w + dpad)
 104             if (lpad < 0) lpad = 0
 105 
 106             # avoid adding trailing spaces at the end of lines
 107             if (j == last) dpad = 0
 108 
 109             printf "%*s%*s%s%*s", lpad, "", ipad, "", v, dpad, ""
 110         }
 111 
 112         printf "\n"
 113     }
 114 }