如何在 bash 中打印一些文本并用空格将其填充到一定宽度?

发布于 2024-11-15 12:32:58 字数 825 浏览 2 评论 0原文

我在 bash 脚本中回显一些文本,其中包含一个变量,并且想要填充该变量,以便它始终在右侧具有适当数量的空格,以保持文本的其余部分对齐。

这是我想要的示例:

Echoing random number 1080    [ OK ]
Echoing random number 443     [ OK ]
Echoing random number 34842   [ OK ]

数值的长度不同(可能不超过 5 或 6 位数字)。

我知道 printf 可以通过执行以下操作来做到这一点并右对齐变量:

printf "Echoing random number %5s   [ OK ]" $RAND_NUM

但是,这会像这样格式化文本:

Echoing random number  1080   [ OK ]
Echoing random number   443   [ OK ]
Echoing random number 34842   [ OK ]

当然,仅用空格回显是行不通的:

echo "Echoing random number ${RAND_NUM}   [ OK ]"

产生这个:

Echoing random number 1080   [ OK ]
Echoing random number 443   [ OK ]
Echoing random number 34842   [ OK ]

有没有办法打印文本就像我的第一个例子一样?

I'm echoing some text in a bash script with a variable in it, and want to pad that variable so it will always have the appropriate ammount of spaces to the right to keep the rest of the text aligned.

Here's an example of what I want:

Echoing random number 1080    [ OK ]
Echoing random number 443     [ OK ]
Echoing random number 34842   [ OK ]

The numerical value would be of varying length (probably no longer than 5 or 6 digits).

I know that printf can do this and right align the variable by doing the following:

printf "Echoing random number %5s   [ OK ]" $RAND_NUM

However, this would format the text like this:

Echoing random number  1080   [ OK ]
Echoing random number   443   [ OK ]
Echoing random number 34842   [ OK ]

And of course just echoing with spaces doens't work:

echo "Echoing random number ${RAND_NUM}   [ OK ]"

Produces this:

Echoing random number 1080   [ OK ]
Echoing random number 443   [ OK ]
Echoing random number 34842   [ OK ]

Is there a way to print the text like my first example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

猥︴琐丶欲为 2024-11-22 12:32:59

使用 - 左对齐字段。

printf "Echoing random number %-5s   [ OK ]" $RAND_NUM

或者,如果您使用的是 Red Hat Linux 系统,则有预定义的函数将打印出绿色 OK 和红色 FAILED 提示(您在启动过程中看到的提示):

#!/bin/bash

. /etc/init.d/functions

echo -n "Frobbing widget:"
frob_widget && echo_success || echo_failure
echo

Use - to left align a field.

printf "Echoing random number %-5s   [ OK ]" $RAND_NUM

Alternatively, if you're on a Red Hat Linux system there are predefined functions that will print out green OK and red FAILED prompts (the ones you see during bootup):

#!/bin/bash

. /etc/init.d/functions

echo -n "Frobbing widget:"
frob_widget && echo_success || echo_failure
echo
红尘作伴 2024-11-22 12:32:59

将所有行收集在一个 var 或文本文件中,然后通过 column 命令进行管道传输。
所以这个(我的示例文件/tmp/columns.txt

Echoing random number 1080 [ OK ]
Echoing random number 44332356 [ OK ]
Echoing random number 34842 [ OK ]
Echoing random number 342 [ OK ]

变成了这个

Echoing  random  number  1080      [  OK  ]
Echoing  random  number  44332356  [  OK  ]
Echoing  random  number  34842     [  OK  ]
Echoing  random  number  342       [  OK  ]

示例命令:cat /tmp/columns.txt |列-t

Collect all your lines in one var or text file then pipe it through column command.
So this (my example file /tmp/columns.txt)

Echoing random number 1080 [ OK ]
Echoing random number 44332356 [ OK ]
Echoing random number 34842 [ OK ]
Echoing random number 342 [ OK ]

became this

Echoing  random  number  1080      [  OK  ]
Echoing  random  number  44332356  [  OK  ]
Echoing  random  number  34842     [  OK  ]
Echoing  random  number  342       [  OK  ]

Example command: cat /tmp/columns.txt | column -t

影子的影子 2024-11-22 12:32:59

简单的标准 Bash 函数($1:要填充的字符串;$2:整数填充长度,左填充为正,右填充为负):

function pad () { [ "$#" -gt 1 ] & & [ -n "$2" ] && printf "%$2.${2#-}s" "$1"; }

使用示例:

$ echo "!$(pad "foobar" 9)!"
!   foobar!
$ echo "!$(pad "foobar" -9)!"
!foobar   !
$ echo "!$(pad "foobar" 3)!"
!foo!

Simple standard Bash function ($1: string to pad; $2: integer padding-length, positive for left-padding, negative for right-padding):

function pad () { [ "$#" -gt 1 ] && [ -n "$2" ] && printf "%$2.${2#-}s" "$1"; }

Usage examples:

$ echo "!$(pad "foobar" 9)!"
!   foobar!
$ echo "!$(pad "foobar" -9)!"
!foobar   !
$ echo "!$(pad "foobar" 3)!"
!foo!
或十年 2024-11-22 12:32:59

扩展 sobi3ch 的答案:如果您使用分隔符连接字符串(我使用 tilda (~)),则可以使用 -s 参数调用 column 来在该点拆分文本。

对虐待猫行为表示歉意:

foo.txt :

Echoing random number 1080~[ OK ]
Echoing random number 1080~[ OK ]
Echoing random number 1080~[ Failed ]

那么:

cat foo.txt | column -s'~'

Echoing random number 1080        [ OK ]
Echoing random number 1080        [ OK ]
Echoing random number 1080        [ Failed ]

To expand on sobi3ch's answer: if you concat the strings with a deliminator (I use tilda (~)), you can then call column with the -s param to split the text at that point.

Apologies for the feline abuse:

foo.txt :

Echoing random number 1080~[ OK ]
Echoing random number 1080~[ OK ]
Echoing random number 1080~[ Failed ]

then :

cat foo.txt | column -s'~'

Echoing random number 1080        [ OK ]
Echoing random number 1080        [ OK ]
Echoing random number 1080        [ Failed ]
厌倦 2024-11-22 12:32:59
# Padding with spaces:

x_padded="    $x"
x_padded=${x_padded: -3}

# Padding with zeros:

x_padded="000$x"
x_padded=${x_padded: -3}
# Padding with spaces:

x_padded="    $x"
x_padded=${x_padded: -3}

# Padding with zeros:

x_padded="000$x"
x_padded=${x_padded: -3}
双马尾 2024-11-22 12:32:59

对于文本的一般情况,也可以使用 sed:

num_padded = $(echo "${RAND_NUM}       " | sed -E 's/(.{8}).*/\1/') # keeps first 8 chars
echo "Echoing random number $num_padded [ OK ]"

For the general case of text too, use sed:

num_padded = $(echo "${RAND_NUM}       " | sed -E 's/(.{8}).*/\1/') # keeps first 8 chars
echo "Echoing random number $num_padded [ OK ]"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文