均匀对齐列,无需“column - t”

均匀对齐列,无需“column - t”

我有一个从 Korn shell 脚本创建的输出文件。我需要将输出与空格均匀对齐。不幸的column -t是在 AIX 上不可用。

实际文件:

     X1vir1000      x1-DFB127   x1ttcb101_mv_03_2016   Not-activated  16         x1ttcr181   x1ttcr182
    X1vir1000       x1-DFB127   x1dvdb201_mv_pmp-132   Not-activated  3          x1ttcr181   x1ttcr182
    X3vir1000       x3-DFB116   x3dvdb202_mv_05032016   Not-activated  22         x3ttcr175   x3ttcr176
    X3vir1000       x3-DFB116   x3ttcb203_mv_03_2016   Not-activated  19         x3ttcr175   x3ttcr176
    X3vir1000       x3-DFB116   x3trcb223_mv_10_2017   Not-activated  29         x3ttcr175   x3ttcr176
    X3vir1000       x3-DFB117   x3trvf245_mv_08_2017   Not-activated  27         x3ttcr177   x3ttcr178
    X3vir1000      x3-DFB131   CR74536_x3dvap234_Decom   Not-activated  101        x3ttcr213   x3ttcr214
    X3vir1000      x3-DFB132   decommissioned_x3trcb223   Not-activated  138        x3ttcr217   x3ttcr218
    X3vir1000      x3-DFB132   decommissioned_x3trcb312   Not-activated  116        x3ttcr217   x3ttcr218
    X9vir1000       x9-DFB005-8233-E8B-SN1030BDR x9trcb003_vhost1_decomm   Not-activated  4          x9ttcr012   x9ttcr013


Expected Result: 


    X1vir1000   x1-DFB127                       x1ttcb101_mv_03_2016       Not-activated  16         x1ttcr181   x1ttcr182
    X1vir1000   x1-DFB127                       x1dvdb201_mv_pmp-132       Not-activated  3          x1ttcr181   x1ttcr182
    X3vir1000   x3-DFB116                       x3dvdb202_mv_05032016      Not-activated  22         x3ttcr175   x3ttcr176
    X3vir1000   x3-DFB116                       x3ttcb203_mv_03_2016       Not-activated  19         x3ttcr175   x3ttcr176
    X3vir1000   x3-DFB116                       x3trcb223_mv_10_2017       Not-activated  29         x3ttcr175   x3ttcr176
    X3vir1000   x3-DFB117                       x3trvf245_mv_08_2017       Not-activated  27         x3ttcr177   x3ttcr178
    X3vir1000   x3-DFB131                       CR74536_x3dvap234_Decom    Not-activated  101        x3ttcr213   x3ttcr214
    X3vir1000   x3-DFB132                       decommissioned_x3trcb223   Not-activated  138        x3ttcr217   x3ttcr218
    X3vir1000   x3-DFB132                       decommissioned_x3trcb312   Not-activated  116        x3ttcr217   x3ttcr218
    X9vir1000   x9-DFB005-8233-E8B-SN1030BDR    x9trcb003_vhost1_decomm    Not-activated  4          x9ttcr012   x9ttcr013

答案1

FNR == NR {
    for (i = 1; i <= NF; ++i) {
        width = length($i)
        maxwidth[i] = (width > maxwidth[i] ? width : maxwidth[i])
    }

    next
}

{
    for (i = 1; i <= NF; ++i)
        printf("%-*s%s", maxwidth[i], $i,
            (i == NF ? "\n" : (delim == "" ? " " : delim)) )
}

awk脚本期望读取同一文件两次。第一次,它记录输入数据中每列的最大宽度。第二次,它打印格式化为该最大宽度的列。

如果delim设置了该变量,则使用它来分隔列,否则使用空格字符。

默认情况下假设原始数据以空格分隔。如果是制表符分隔,则-F '\t'在命令行中使用。

对给定数据运行两次测试(请注意,必须指定输入文件两次):

$ awk -f ./script.awk file file
X1vir1000 x1-DFB127                    x1ttcb101_mv_03_2016     Not-activated 16  x1ttcr181 x1ttcr182
X1vir1000 x1-DFB127                    x1dvdb201_mv_pmp-132     Not-activated 3   x1ttcr181 x1ttcr182
X3vir1000 x3-DFB116                    x3dvdb202_mv_05032016    Not-activated 22  x3ttcr175 x3ttcr176
X3vir1000 x3-DFB116                    x3ttcb203_mv_03_2016     Not-activated 19  x3ttcr175 x3ttcr176
X3vir1000 x3-DFB116                    x3trcb223_mv_10_2017     Not-activated 29  x3ttcr175 x3ttcr176
X3vir1000 x3-DFB117                    x3trvf245_mv_08_2017     Not-activated 27  x3ttcr177 x3ttcr178
X3vir1000 x3-DFB131                    CR74536_x3dvap234_Decom  Not-activated 101 x3ttcr213 x3ttcr214
X3vir1000 x3-DFB132                    decommissioned_x3trcb223 Not-activated 138 x3ttcr217 x3ttcr218
X3vir1000 x3-DFB132                    decommissioned_x3trcb312 Not-activated 116 x3ttcr217 x3ttcr218
X9vir1000 x9-DFB005-8233-E8B-SN1030BDR x9trcb003_vhost1_decomm  Not-activated 4   x9ttcr012 x9ttcr013
$ awk -v delim=' | ' -f ./script.awk file file
X1vir1000 | x1-DFB127                    | x1ttcb101_mv_03_2016     | Not-activated | 16  | x1ttcr181 | x1ttcr182
X1vir1000 | x1-DFB127                    | x1dvdb201_mv_pmp-132     | Not-activated | 3   | x1ttcr181 | x1ttcr182
X3vir1000 | x3-DFB116                    | x3dvdb202_mv_05032016    | Not-activated | 22  | x3ttcr175 | x3ttcr176
X3vir1000 | x3-DFB116                    | x3ttcb203_mv_03_2016     | Not-activated | 19  | x3ttcr175 | x3ttcr176
X3vir1000 | x3-DFB116                    | x3trcb223_mv_10_2017     | Not-activated | 29  | x3ttcr175 | x3ttcr176
X3vir1000 | x3-DFB117                    | x3trvf245_mv_08_2017     | Not-activated | 27  | x3ttcr177 | x3ttcr178
X3vir1000 | x3-DFB131                    | CR74536_x3dvap234_Decom  | Not-activated | 101 | x3ttcr213 | x3ttcr214
X3vir1000 | x3-DFB132                    | decommissioned_x3trcb223 | Not-activated | 138 | x3ttcr217 | x3ttcr218
X3vir1000 | x3-DFB132                    | decommissioned_x3trcb312 | Not-activated | 116 | x3ttcr217 | x3ttcr218
X9vir1000 | x9-DFB005-8233-E8B-SN1030BDR | x9trcb003_vhost1_decomm  | Not-activated | 4   | x9ttcr012 | x9ttcr013

awk嵌入上述程序并采用两个选项的shell 脚本:

  • -d delim其中delim是要使用的输出分隔符。
  • -D delim其中delim是要使用的输入分隔符(例如-D '\t'输入数据中的制表符)。

该脚本将像这样使用来重新创建上面的两次运行:

./script.sh file
./script.sh -d ' | ' file

剧本:

#!/bin/sh

while getopts 'd:D:' opt; do
    case $opt in
        d)  delim=$OPTARG ;;
        D)  fs=$OPTARG ;;
        *)  echo 'Error in command line parsing' >&2
            exit 1
    esac
done
shift "$(( OPTIND - 1 ))"

tmpfile=$(mktemp)
# If mktemp is not available:
# tmpfile="${TMPDIR:-/tmp}/columnn-t.tmp"
# ... or something similar
cat "$1" >$tmpfile

awk ${delim:+-v delim="$delim"} ${fs:+-F "$fs"} '
FNR == NR {
    for (i = 1; i <= NF; ++i) {
        width = length($i)
        maxwidth[i] = (width > maxwidth[i] ? width : maxwidth[i])
    }

    next
}

{
    for (i = 1; i <= NF; ++i)
        printf("%-*s%s", maxwidth[i], $i,
            (i == NF ? "\n" : (delim == "" ? " " : delim)) )    
}' "$tmpfile" "$tmpfile"

rm -f "$tmpfile"

相关内容