首页 > 解决方案 > 如何使用python对齐linux输出

问题描述

我正在尝试使bash命令的输出对齐。

这是我现在使用的命令。我使用执行此bash命令python并且它工作正常,所以我不打算发布该部分。

kubectl get pods -n kube-system | awk '/Completed/ {print $1}'

我得到这个输出到一个名为的变量output

这就是它的输出

akamai-1576314300-xhf78         Completed                                                                                                                                                                          
akamai-1576659900-npb6g         Completed
akamai-1576746300-6vdjm         Completed
keycloak-setupbhnhc-zskhw               Completed
vault-renew-tokens-1576378800-6dp98             Completed

这是我通常使用打印时的外观print(output)

但我正在output像这样使用这个变量。

print("here is the output: "+constant.ICON+output+")

我的输出现在看起来像这样:

 - here is the output:                                                                                                                                                                                    
             ● - akamai-1576227900-wrkct               Completed
akamai-1576314300-xhf78         Completed                                                                                                                                                                          
akamai-1576400700-6m84q         Completed
akamai-1576487100-bnrg7         Completed
akamai-1576573500-g48lq         Completed
akamai-1576659900-npb6g         Completed
akamai-1576746300-6vdjm         Completed
keycloak-setupbhnhc-zskhw               Completed
vault-renew-tokens-1576378800-6dp98             Completed

它没有正确对齐。

我真正想要的是这样的:

 - here is the output:                                                                                                                                                                                   
             ● - akamai-1576227900-wrkct         Completed
                 akamai-1576314300-xhf78         Completed                                                                                                                                                                          
                 akamai-1576400700-6m84q         Completed
                 akamai-1576487100-bnrg7         Completed
                 akamai-1576573500-g48lq         Completed
                 akamai-1576659900-npb6g         Completed
                 akamai-1576746300-6vdjm         Completed

我怎样才能做到这一点?

我正在考虑将bash命令输出带到一个数组中,以便我可以正确执行此操作,但我无法将输出输出到array

有什么好的方法可以做到这一点吗?

标签: pythonlinuxbashcommand-line

解决方案


无需使用 python,只需将其通过管道传输到column -t

kubectl get pods -n kube-system | awk '/Completed/ {print $1}' | column -t

例如

$ cat file.txt
akamai-1576314300-xhf78         Completed                                                                                                                                                                          
akamai-1576659900-npb6g         Completed
akamai-1576746300-6vdjm         Completed
keycloak-setupbhnhc-zskhw               Completed
vault-renew-tokens-1576378800-6dp98             Completed

-

$ cat file.txt | column -t
akamai-1576314300-xhf78              Completed
akamai-1576659900-npb6g              Completed
akamai-1576746300-6vdjm              Completed
keycloak-setupbhnhc-zskhw            Completed
vault-renew-tokens-1576378800-6dp98  Completed

推荐阅读