如何在 bash 中打印一些文本并用空格填充到一定宽度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6345429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I print some text in bash and pad it with spaces to a certain width?
提问by PHLAK
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.
我在带有变量的 bash 脚本中回显一些文本,并希望填充该变量,以便它始终在右侧有适当的空格数量,以保持文本的其余部分对齐。
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).
数值将具有不同的长度(可能不超过 5 或 6 位数字)。
I know that printf can do this and right align the variable by doing the following:
我知道 printf 可以做到这一点,并通过执行以下操作正确对齐变量:
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?
有没有办法像我的第一个例子一样打印文本?
回答by John Kugelman
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):
或者,如果您使用的是 Red Hat Linux 系统,则有一些预定义的函数会打印出绿色 OK 和红色 FAILED 提示(您在启动期间看到的提示):
#!/bin/bash
. /etc/init.d/functions
echo -n "Frobbing widget:"
frob_widget && echo_success || echo_failure
echo
回答by sobi3ch
Collect all your lines in one var or text file then pipe it through column
command.
So this (my example file /tmp/columns.txt
)
将所有行收集在一个 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 ]
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
示例命令: cat /tmp/columns.txt | column -t
回答by Steve Almond
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.
要扩展 sobi3ch 的答案:如果您使用分隔符连接字符串(我使用 tilda (~)),那么您可以使用 -s 参数调用 column 以在该点拆分文本。
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 ]