printf 中的填充字符
我正在编写一个 bash shell 脚本来显示进程是否正在运行。
到目前为止,我得到了这个:
printf "%-50s %s\n" $PROC_NAME [UP]
代码给了我这个输出:
JBoss [DOWN]
GlassFish [UP]
verylongprocessname [UP]
我想用“-”或“*”填充两个字段之间的间隙,以使其更具可读性。如何在不影响字段对齐的情况下做到这一点?
我想要的输出是:
JBoss ------------------------------------------- [DOWN]
GlassFish --------------------------------------- [UP]
verylongprocessname ----------------------------- [UP]
I am writing a bash shell script to display if a process is running or not.
So far, I got this:
printf "%-50s %s\n" $PROC_NAME [UP]
The code gives me this output:
JBoss [DOWN]
GlassFish [UP]
verylongprocessname [UP]
I want to pad the gap between the two fields with a '-' or '*' to make it more readable. How do I do that without disturbing the alignment of the fields?
The output I want is:
JBoss ------------------------------------------- [DOWN]
GlassFish --------------------------------------- [UP]
verylongprocessname ----------------------------- [UP]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
纯 Bash,无外部实用程序
此演示提供了完整的理由,但如果您想要右侧参差不齐的行,则可以省略减去第二个字符串的长度。
不幸的是,使用这种技术,填充字符串的长度必须被硬编码为比您认为需要的最长长度更长,但是
padlength
可以是一个变量,如图所示。但是,您可以用这三行替换第一行,以便能够使用变量来表示 pad 的长度:因此 pad (
padlimit
和padlength
) 可以基于取决于终端宽度 ($COLUMNS
) 或根据最长数据字符串的长度计算。输出:
不减去第二个字符串的长度:
第一行可以是等效的(类似于
sprintf
):或者类似地对于更动态的技术:
或者这个(允许多字符“省略号” " 无需修改格式字符串来容纳字符数 - 上例中的
.1
)。它假定名称为$_1
、$_2
等的变量未设置或为空。:如果您愿意,可以将所有内容打印在一行上:
Pure Bash, no external utilities
This demonstration does full justification, but you can just omit subtracting the length of the second string if you want ragged-right lines.
Unfortunately, with that technique, the length of the pad string has to be hardcoded to be longer than the longest one you think you'll need, but the
padlength
can be a variable as shown. However, you can replace the first line with these three to be able to use a variable for the length of the pad:So the pad (
padlimit
andpadlength
) could be based on terminal width ($COLUMNS
) or computed from the length of the longest data string.Output:
Without subtracting the length of the second string:
The first line could instead be the equivalent (similar to
sprintf
):Or similarly for the more dynamic technique:
Or this (which allows multi-character "ellipses" without having to modify the format string to accommodate the number of characters -
.1
in the example above). It assumes that variables with names such as$_1
,$_2
, etc., are unset or empty.:You can do the printing all on one line if you prefer:
纯粹的巴什。使用“PROC_NAME”值的长度作为固定字符串“line”的偏移量:
这给出
Pure Bash. Use the length of the value of 'PROC_NAME' as offset for the fixed string 'line':
This gives
简单(但有效)的解决方案:
Trivial (but working) solution:
我认为这是最简单的解决方案。纯 shell 内置函数,没有内联数学。它借鉴了以前的答案。
只是子字符串和 ${#...} 元变量。
生产
产品 生产产品
I think this is the simplest solution. Pure shell builtins, no inline math. It borrows from previous answers.
Just substrings and the ${#...} meta-variable.
Produces
Produces
简单但确实有效:
使用示例:
输出到标准输出:
Simple but it does work:
Example of usage:
Output to stdout:
使用
printf
无法填充除空格之外的任何内容。您可以使用 sed:There's no way to pad with anything but spaces using
printf
. You can usesed
:说明:
printf '\055%.0s' {1..40}
- 创建 40 个破折号(破折号被解释为选项,因此请使用转义的 ascii 代码)
"$PROC_NAME ..."
- 连接 $PROC_NAME 和破折号| head -c 40
- 将字符串修剪为前 40 个字符Explanation:
printf '\055%.0s' {1..40}
- Create 40 dashes(dash is interpreted as option so use escaped ascii code instead)
"$PROC_NAME ..."
- Concatenate $PROC_NAME and dashes| head -c 40
- Trim string to first 40 chars这个更简单并且不执行外部命令。
This one is even simpler and execs no external commands.
仅使用
echo
@Dennis Williamson 的 anwser 工作得很好,除了我试图使用 echo 来做到这一点。 Echo 允许输出具有特定颜色的字符。使用 printf 会删除该颜色并打印不可读的字符。这是仅
echo
的替代方案:输出:
当然,您可以使用 @Dennis Williamson 提出的所有变体,无论您希望右侧部分左对齐还是右对齐(替换
25 - ${#string1}
by25 - ${#string1} - ${#string2}
等...using
echo
onlyThe anwser of @Dennis Williamson is working just fine except I was trying to do this using echo. Echo allows to output charcacters with a certain color. Using printf would remove that coloring and print unreadable characters. Here's the
echo
-only alternative:output:
of course you can use all the variations proposed by @Dennis Williamson whether you want the right part to be left- or right-aligned (replacing
25 - ${#string1}
by25 - ${#string1} - ${#string2}
etc...这是另一个:
Here's another one:
如果您要在某个固定的列号处结束填充字符,那么您可以过度填充并
剪切
到长度:If you are ending the pad characters at some fixed column number, then you can overpad and
cut
to length:具有自动缩放/调整大小方法和示例的简单控制台跨度/填充/填充/填充。
示例:
create-console-spanner“正在加载图形模块”“[成功]”
现在这是一个全功能颜色-character-terminal-suite 完成与使用扳手打印颜色和样式格式化字符串有关的所有操作。
要打印颜色,这很简单:
paint-format "&red;This is %s\n" red
稍后您可能想要加粗:
paint-format "&bold;%s!\n" WOW
paint- 的
函数测量文本,以便您可以执行控制台字体度量操作。-l
选项formatpaint-format
函数的-v
选项与printf
的作用相同,但不能与-l
一起提供现在进行跨越!
paint-span“你好”。 " &blue;world"
[注意:我们没有添加换行符终端序列,但文本填充了终端,因此下一行仅显示为换行符终端序列],其输出为:
你好......................世界
Simple Console Span/Fill/Pad/Padding with automatic scaling/resizing Method and Example.
Example:
create-console-spanner "loading graphics module" "[success]"
Now here is a full-featured-color-character-terminal-suite that does everything in regards to printing a color and style formatted string with a spanner.
To print a color, that's simple enough:
paint-format "&red;This is %s\n" red
And you might want to get bold later on:
paint-format "&bold;%s!\n" WOW
The
-l
option to thepaint-format
function measures the text so you can do console font metrics operations.The
-v
option to thepaint-format
function works the same asprintf
but cannot be supplied with-l
Now for the spanning!
paint-span "hello " . " &blue;world"
[note: we didn't add newline terminal sequence, but the text fills the terminal, so the next line only appears to be a newline terminal sequence]and the output of that is:
hello ............................. world
Bash + seq 允许参数扩展
与 @Dennis Williamson 答案类似,但如果
seq
可用,则不需要对填充字符串的长度进行硬编码。以下代码允许将变量作为位置参数传递给脚本:使用 ASCII 代码“2D”而不是字符“-”以避免 shell 将其解释为命令标志。另一个选项是“3D”使用“=”。
如果没有任何 padlength 作为参数传递,上面的代码默认为 80 个字符的标准终端宽度。
要利用 bash shell 变量
COLUMNS
(即当前终端的宽度),环境变量需要可供脚本使用。一种方法是通过执行前面带有.
(“dot”命令)的脚本来获取所有环境变量,如下所示:或者(更好)在以下情况下显式传递
COLUMNS
变量:执行,像这样:Bash + seq to allow parameter expansion
Similar to @Dennis Williamson answer, but if
seq
is available, the length of the pad string need not be hardcoded. The following code allows for passing a variable to the script as a positional parameter:The ASCII code "2D" is used instead of the character "-" to avoid the shell interpreting it as a command flag. Another option is "3D" to use "=".
In absence of any padlength passed as an argument, the code above defaults to the 80 character standard terminal width.
To take advantage of the the bash shell variable
COLUMNS
(i.e., the width of the current terminal), the environment variable would need to be available to the script. One way is to source all the environment variables by executing the script preceded by.
("dot" command), like this:or (better) explicitly pass the
COLUMNS
variable when executing, like this:如果您希望在保持 bash 3.2 兼容(对于 MacOS)的同时获得更多的稳健性,我在这些情况下经常使用与此类似的模式:
输出:
它非常冗长,但使用这样的模式,您可以通过在要对齐的变量的右侧或左侧添加诸如
key_align_right
之类的变量来控制右对齐或左对齐,然后添加"${key_align_right:${#tools[i]}}"
用空格字符串减去对齐变量的长度,从而对齐变量。您还可以在循环内使用这段逻辑
value_trim=$(echo "${versions[i]}" | awk '{ print substr($0, 1, '"${#padding_right}"') } ' )
以防止变量超出您的范围。如果您的示例中的表格具有外部边界,一旦超出外部边界,就会在视觉上破坏视图,这将非常有用。该逻辑将打印该值的子字符串,该子字符串不能长于 padding_right 长度。您可以在输出中看到我的 bash 版本值比表的边界长,但不会破坏它。这还允许您通过调整打印到顶部变量的空格数、调整范围中的最后一个数字来轻松调整填充、边距和最大长度。
If you'd like for a little bit more robustness while keeping things bash 3.2 compliant ( for MacOS) I use a pattern similar to this pretty frequently for these situations:
Output:
It is very verbose but using a pattern like this you can control right or left align by adding variables like
key_align_right
to the right or left of the variable you'd like to align, and then adding"${key_align_right:${#tools[i]}}"
to subtract the length of the aligned variable by the string of spaces, aligning the variable.You can also use this piece of logic inside the loop
value_trim=$(echo "${versions[i]}" | awk '{ print substr($0, 1, '"${#padding_right}"') }' )
to keep variables from going passed your bounds. This is extremely helpful if you have something like the table in my example that has an outer bound that once crossed will visually break the view. The logic will print a substring of the value that can be no longer than the padding_right length. You can see in the output that my value of bash version is longer than the bounds of my table, but doesn't break it.This also allows you to easily adjust the padding, margins and max length by adjusting the the number of spaces printed to the variables at the top, adjusting the last number in the range.