【问题标题】:How to format columns with printf in bash when tput is used使用 tput 时如何在 bash 中使用 printf 格式化列
【发布时间】:2020-01-03 00:42:12
【问题描述】:

使用 tput 会更改字符串的长度,因此列不对齐。如何解决这个问题?

在 bash 脚本中尝试了以下代码。

B="$(tput bold)" # Bold text
N="$(tput sgr0)" # Normal text

function testing(){

IN="KEYS | VALUES | DESCRIPTION
id | ${B}10${N} or ${B}20${N} | Enter ID. Default is ${B}10${N}.
status | ${B}true${N} or ${B}false${N} | Enter status of something. Default is ${B}true${N}.
style | Example: Standard | Give suitable standard."

    IFS= 
    while read -r lines; do
        IFS='|' read -r -a array <<< "$lines"
        printf "%-35s %-35s %-s\n" "${array[0]}" "${array[1]}" "${array[2]}"
    done <<< "$IN"

    read -p "$*"
    exit 0
}

输出类似于:

    KEYS          VALUES                              DESCRIPTION
    id            **10** or **20**           Enter ID. Default is **10**.
    status        **true** or **false**      Enter status of something. Default is **true**.
    style         Example: Standard                   Give suitable standard.

预计是:

    KEYS          VALUES                              DESCRIPTION
    id            **10** or **20**                    Enter ID. Default is **10**.
    status        **true** or **false**               Enter status of something. Default is **true**.
    style         Example: Standard                   Give suitable standard.

【问题讨论】:

  • 当我在我的 bash 环境中运行代码时,我发现 2 个有问题的 DESCRIPTION 字符串失去了 20 个缩进空格; vars (${#B} & ${#N}) 的长度分别为 4 和 6 个字符;这些变量在VALUES 字段中出现两次,总共有 20 个(不可打印)字符; printf/%s 将 20 个不可打印的字符计为输出的一部分,因此出现 20 个空格“丢失”(即,printf is 打印 35 个字符......只是其中的 20 个这些字符是非打印的); “呃,马克!” ?我不知道的是:printf 是否有一个标志可以忽略不可打印的字符以用于格式目的?

标签: bash formatting printf tput


【解决方案1】:

正如我在评论中提到的:

  • 当我在 bash 环境中运行代码时,我发现 2 个有问题的 DESCRIPTION 字符串丢失了 20 个缩进空格
  • 变量 (${#B} & ${#N}) 的长度分别为 4 和 6 个字符
  • 这些变量在 VALUES 字段中出现两次,总共 20 个(不可打印)字符
  • printf/%s 将 20 个不可打印字符计为输出的一部分,因此出现 20 个空格“丢失”(即,printf is 打印 35 个字符......只是其中的 20 个这些字符是非打印的)

如果我们测量第二个字段的长度会怎样 - 有和没有特殊字符 - 然后用差值(即不可打印字符的个数)增加printf命令第二个字段的格式宽度?

我们将第二个字段的长度(包括我们的特殊字符)存储在一个新变量len2

len2="${#array[1]}"

接下来我们要去掉特殊字符并测量结果字符串的长度并放入变量lenx

x="${array[1]//${B}/}"    # strip out all occurrences of variable 'B'
x="${x//${N}/}"           # strip out all occurrences of variable 'N'
lenx=${#x}

注意:我在让trsed 正确去除特殊字符时遇到了一些问题;我愿意接受建议。从好的方面来说......我没有产生任何子进程。

我们将新的格式宽度存储在变量w2(字段'2'的'w'idth)中,如下所示:

w2=$(( 35 + len2 - lenx ))

而新的printf格式字符串变为:

printf "%-35s %-${w2}s %-s\n" ...

综合起来我们得到:

B="$(tput bold)" # Bold text
N="$(tput sgr0)" # Normal text

function testing(){

IN="KEYS | VALUES | DESCRIPTION
id | ${B}10${N} or ${B}20${N} | Enter ID. Default is ${B}10${N}.
status | ${B}true${N} or ${B}false${N} | Enter status of something. Default is ${B}true${N}.
style | Example: Standard | Give suitable standard."

    IFS= 
    while read -r lines; do
        IFS='|' read -r -a array <<< "$lines"

        len2="${#array[1]}"

        x="${array[1]//${B}/}"
        x="${x//${N}/}"
        lenx=${#x}

        w2=$(( 35 + len2 - lenx ))
   #    echo "w2 = ${w2}"

        printf "%-35s %-${w2}s %-s\n" "${array[0]}" "${array[1]}" "${array[2]}"
    done <<< "$IN"

    read -p "$*"
    exit 0
}

在我的 bash 环境中运行脚本会生成:

$ testing
KEYS                                 VALUES                              DESCRIPTION
id                                   10 or 20                            Enter ID. Default is 10.
status                               true or false                       Enter status of something. Default is true.
style                                Example: Standard                   Give suitable standard.

注意:在我的终端上,粗体字段确实打印为 bold ...它们只是不会在上述答案中复制/显示为 bold

如果您取消注释该行 - echo "w2 = ${w2}" - 您应该会发现(对于感兴趣的 2 行)我们将为第二个字段使用 55 的格式宽度(即所需的 35加上一个额外的 20 来补偿 20 个字符的不可打印字符)。

【讨论】:

  • 您的解决方案完全符合需要。谢谢您的帮助!希望将来在这种情况下会有一个 printf 标志。
  • 很高兴听到,尽管我们怀疑我们是否会在任何时候(很快)看到这样的标志
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-19
  • 2020-07-16
相关资源
最近更新 更多