Alinha colunas uniformemente sem “column - t”

1

Eu tenho um arquivo de saída criado a partir de um script de shell Korn. Eu preciso alinhar a saída com espaços uniformemente. Infelizmente column -t não está disponível no AIX.

Arquivo real:

     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
    
por satsensort 27.11.2018 / 09:12

1 resposta

2
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)) )
}

Este script awk espera ler o mesmo arquivo duas vezes. Na primeira vez, registra a largura máxima de cada coluna nos dados de entrada. Na segunda vez, imprime as colunas formatadas para essa largura máxima.

Se a variável delim estiver definida, ela será usada para delimitar as colunas, caso contrário, será usado um caractere de espaço.

O padrão é assumir que os dados originais são delimitados por espaços em branco. Se for delimitado por tabulação, use -F '\t' na linha de comando.

Dois testes são executados nos dados fornecidos (observe que o arquivo de entrada deve ser especificado duas vezes ):

$ 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

Um script de shell que incorpora o programa awk acima e tem duas opções:

  • -d delim onde delim é o delimitador de saída a ser usado.
  • -D delim onde delim é o delimitador de entrada a ser usado (por exemplo, -D '\t' para guias nos dados de entrada).

O script seria usado assim para recriar as duas execuções acima:

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

O script:

#!/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"
    
por 27.11.2018 / 10:06