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