3

使用 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.
4

1 回答 1

3

正如我在评论中提到的:

  • 当我在我的 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(字段'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.

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

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

于 2019-08-29T19:29:39.113 回答