如何在 bash 中打印一些文本并用空格将其填充到一定宽度?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用
-
左对齐字段。或者,如果您使用的是 Red Hat Linux 系统,则有预定义的函数将打印出绿色 OK 和红色 FAILED 提示(您在启动过程中看到的提示):
Use
-
to left align a field.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):
将所有行收集在一个 var 或文本文件中,然后通过
column
命令进行管道传输。所以这个(我的示例文件
/tmp/columns.txt
)变成了这个
示例命令:
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
)became this
Example command:
cat /tmp/columns.txt | column -t
简单的标准 Bash 函数($1:要填充的字符串;$2:整数填充长度,左填充为正,右填充为负):
function pad () { [ "$#" -gt 1 ] & & [ -n "$2" ] && printf "%$2.${2#-}s" "$1"; }
使用示例:
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:
扩展 sobi3ch 的答案:如果您使用分隔符连接字符串(我使用 tilda (~)),则可以使用 -s 参数调用 column 来在该点拆分文本。
对虐待猫行为表示歉意:
那么:
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:
then :
对于文本的一般情况,也可以使用 sed:
For the general case of text too, use sed: