繁体   English   中英

右对齐空白列(bash)?

[英]Right align empty column (bash)?

我有一个打印列的脚本,但是如果左列为空,它将无法正确对齐。

现在,我正在遍历数组并打印出键/值,并使用column命令格式化列。 下面是代码的样子。

# Code to make the array
declare -A pods
declare -A associative_array
pods=$(kubectl get pods | awk '{if(NR>1)print $1}')
for p in ${pods[*]}; do
    image=$(kubectl get pod "$p" -o json | jq -r '.spec.containers[].image')
    associative_array[$p]+="$image"
done

# Code to print the array
(printf "column1\tcolumn2\n"

for i in "${!associative_array[@]}"; do
    printf '%s\t%s\n' "$i" "${associative_array[$i]}"
done) | column -t -x
...

这是当前输出的示例。

column1                                           column2
prometheus-k8s-0                                  carlosedp/prometheus:v2.7.1
carlosedp/prometheus-config-reloader:v0.28.0
carlosedp/configmap-reload:v0.2.2

如果第一列为空,是否有一种简单的方法可以使文本正确对齐?

更新:

我发现了问题之一,并更新了代码以显示如何创建阵列。 我用来创建第一个数组的命令添加了换行符。

更新以删除换行符后,输出现在看起来像这样,因此在任何情况下都不存在空键,在某些情况下给定键具有多个值。

column1                               column2
prometheus-k8s-0                      carlosedp/prometheus:v2.7.1 carlosedp/prometheus-config-reloader:v0.28.0 carlosedp/configmap-reload:v0.2.2

看起来您只需要在printf调用的前%s中添加一个长度说明符即可:

$ printf "%-50s%s\n" "$i" "${associative_array[$i]}"

请注意,这摆脱了制表符,该字符不再需要,因为第一列现在用空格右填充,直到其长度为50个字符为止。 另外,我选择50是因为这是前两行中第1列的宽度。

如果您使用这种方法,则还需要删除| column -t -x 最后是| column -t -x ,因为它现在是多余的,并且实际上将撤消您的printf格式,因为它将连续的定界符视为一个定界符。

如果您的column版本支持它,您也可以尝试按原样保留您的printf ,而不是使用column -t -x -s $'\\t' -n ,它告诉column使用\\t作为分隔符,而不是将多个相邻的定界符视为一个定界符:

$ printf "%s\t%s\n" column1 column2 foo bar "" baz | column -t -x -s $'\t' -n
column1  column2
foo      bar
         baz

当然,您可以组合一些选项并缩短

column -t -x -s $'\n' -n

column -txns $'\t'

您当然不能使用关联数组。
您不能分配空密钥。
试试看,看看会发生什么!

declare -A associative_array=( [one]=bar []=truc [three]=foo [four]=baz)
(printf "column1\tcolumn2\n"
for i in "${!associative_array[@]}"; do
  printf '%s\t%s\n' "$i" "${associative_array[$i]}"
done)
echo "number of items = ${#associative_array[@]}"

./script-bash.sh: line 1: []=truc: bad array subscript
column1 column2
four    baz
three   foo
one     bar
number of items = 3

这不是一个完美的解决方案,但是我可以通过检查第二列是否有多个单词,然后将其拆分并在每个字符串上进行迭代来大致获得所需的行为。

for i in "${!associative_array[@]}"; do
    if [[ $(echo "${associative_array[$i]}" | wc -w) -gt 1 ]]; then
        for image in ${associative_array[$i]}; do
            printf '%s\t%s\n' "$i" "$image"
        done
    else
        printf '%s\t%s\n' "$i" "${associative_array[$i]}"
    fi
done) | column -t -x

产生以下输出。

prometheus-k8s-0                      carlosedp/prometheus:v2.7.1
prometheus-k8s-0                      carlosedp/prometheus-config-reloader:v0.28.0
prometheus-k8s-0                      carlosedp/configmap-reload:v0.2.2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM