bash - 使用 tput 时如何在 bash 中使用 printf 格式化列

标签 bash formatting printf tput

使用 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 正在 打印 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 中('w'idth for field '2'),如下所示:

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.

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

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

关于bash - 使用 tput 时如何在 bash 中使用 printf 格式化列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57715240/

相关文章:

iphone - 在Objective-C中将小数转换为分数?

c++ - 使用 fprintf 获取访问冲突写入位置 0x00000014

python - 如何打印整个父-节点-子结构

bash - bash 是否有不短路的 bool 值和/或运算符?

r - 将列表示为百分比 (xts)

linux - bash命令行无法正确读取命令

c - 使用用户确定的填充打印 int 值?

bash - bash 窗口中的水平滚动

linux - 如何仅在命令完成时输出

regex - 为什么 grep 匹配得比应有的多,例如 "^[\s]*set breakpoint.*in"匹配 "set breakpoint 1 at main.c:5"吗?