【问题标题】:Formating a table with columns and row in bash printf在bash printf中格式化包含列和行的表格
【发布时间】:2020-07-16 00:07:34
【问题描述】:

下面是由几个 awk/grep 表达式生成的输出,这些表达式存储在变量中。然后我试图为这些变量的信息绘制一个表格。当我的所有变量都有一个值时,一切正常,并且表格显示格式很好,但是这些变量中有多个值,我的表格就搞砸了。不知道怎么形容。

第 1 列和第 2 列之间的关系是按顺序排列的 1 对 1 映射,因为这就是它们在各自变量中的存储方式。

echo $tl_user_emails
mapping-aws-dev@team.com jhon@team.com
echo $tl_user_nmspcs
test-2d7dbabf-f8cc-4c0b-af3b-80db52d2257e test-60cb4076-9280-4cf9-bb34-9f9e4ac8bd20

printf "\n\n test User Namespaces on node running pods: \n%s" "$tl_user_nmspcs"
printf "\n\n test users running pods on this node:  \n%s\n\n" "$tl_user_emails"
printf "\n${yellow}${bold}%5sTest User Email${creset}${normal}%15s${yellow}${bold}%5sTest User Namespace${creset}${normal}%23s\n" " " "|" " " "|"
printf "%s %16s|%s%5s|\n" "$tl_user_emails" " " "$tl_user_nmspcs" " "
test User Namespaces on node running pods:
test-2d7dbabf-f8cc-4c0b-af3b-80db52d2257e
test-60cb4076-9280-4cf9-bb34-9f9e4ac8bd20




test users running pods on this node:
mapping-aws-dev@team.com
jhon@team.com


Above test pods info on this node: node1

output:
     Test User Email                |     Test User Namespace                      |
mapping-aws-dev@team.com
jhon@team.com.                      |test-2d7dbabf-f8cc-4c0b-af3b-80db52d2257e
test-60cb4076-9280-4cf9-bb34-9f9e4ac8bd20   |
expected output:

     Test User Email                |     Test User Namespace                  |
mapping-aws-dev@team.com            |                                          |
jhon@team.com.                      |test-2d7dbabf-f8cc-4c0b-af3b-80db52d2257e |
                                    |test-60cb4076-9280-4cf9-bb34-9f9e4ac8bd20 |
                                    ||

【问题讨论】:

  • 您在第一列中有一个用户电子邮件列表,在第二列中有一个名称空间列表。两列之间的对应关系是什么? jhon@team.com.test-2d7dbabf-f8cc-4c0b-af3b-80db52d2257e 之间是否存在一些特殊关系,您只将这对放在同一行上?如果是这样,我们如何知道内容必须放在哪一行?
  • 关系是 col1 中的每个条目到 col2 中的每个条目的 1 对 1 映射,这是顺序的,因为这就是它们存储在各个变量中的方式
  • 请使用declare -p varname 生成对变量值的精确描述,并将其包含在问题中。 printf '%s\n' "$varname" 的输出是不够的,因为它没有明确地呈现非打印字符;我们无法判断是否存在转义序列、空格等;而declare -p 的输出是我们可以自己运行以在本地系统上定义相同变量的代码,从而使人们能够自信地测试他们的答案。
  • 好的:我更新了在运行过程中呼应它们的问题

标签: bash printf


【解决方案1】:

试试:

#!/bin/bash

yellow=$(tput setaf 3)
bold=$(tput bold)
creset=$(tput rmso)
normal=$(tput sgr0)

tl_user_emails='mapping-aws-dev@team.com
jhon@team.com'
tl_user_nmspcs='test-2d7dbabf-f8cc-4c0b-af3b-80db52d2257e
test-60cb4076-9280-4cf9-bb34-9f9e4ac8bd20'

printf "\n${yellow}${bold}%5sTest User Email%16s${creset}${normal}|${yellow}${bold}%5sTest User Namespace%18s${creset}${normal}|\n" # all args after format are empty strings by default

declare -a emails
declare -a namespaces

for e in $tl_user_emails; do emails+=($e); done
for n in $tl_user_nmspcs; do namespaces+=($n); done

format="%-36s|%-42s|\n"

i=0
for e in ${emails[@]}; do
  printf "$format" "$e" "${namespaces[((i++))]}"
done

它作为输出提供(在需要的地方使用黄色和粗体字符):


     Test User Email                |     Test User Namespace                  |
mapping-aws-dev@team.com            |test-2d7dbabf-f8cc-4c0b-af3b-80db52d2257e |
jhon@team.com                       |test-60cb4076-9280-4cf9-bb34-9f9e4ac8bd20 |

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 2021-03-23
    • 2010-10-06
    相关资源
    最近更新 更多