printf中填充字符 [英] Padding characters in printf
问题描述
我如果一个进程正在运行或不写一个bash shell脚本来显示。
到目前为止,我'在这里。
的printf%-50s%S \\ n$ proc_name中[UP]
这给了我一个像输出:
的JBoss [DOWN]GlassFish的[UP]verylongprocessname [UP]
我要垫有2场的差距' - '或'*',使其更具可读性。我该怎么做,没有干扰领域的定位?
我想要的输出是:
的JBoss ------------------------------------- - - - [下]GlassFish的--------------------------------------- [UP]verylongprocessname ----------------------------- [UP]
纯猛砸,没有外部应用
本演示内容充分的理由,但你可以省略减去第二个字符串的长度,如果你想衣衫褴褛行权。
垫= $(printf的'%0.1秒' - {1..60})
padlength = 40
字符串2 ='BBBBBBB
一种用于在氨基酸AAAA AAAAAAAA字符串1
做
printf的'%s'的$字符串1
printf的%* s'的0 $((padlength - $ {#字符串1} - $ {#字符串2}))$垫
printf的'%s的\\ n'$字符串2
字符串2 = $ {字符串2:1}
DONE
不幸的是,垫串的长度必须是硬$ C $光盘,但padlength可以是变量,如图所示。
输出:
A -------------------------------- BBBBBBB
AA -------------------------------- BBBBBB
AAAA ------------------------------- BBBBB
AAAAAAAA ---------------------------- BBBB
在不减去第二字符串的长度:
A ------------------------------------- --bbbbbbb
AA -------------------------------------- BBBBBB
AAAA ------------------------------------ BBBBB
AAAAAAAA -------------------------------- BBBB
第一行也可以是符合等价(类似于的sprintf
)
的printf -v垫'%0.1秒' - {} 1..60
您可以做印刷都在同一行,如果你preFER:
的printf'%s的%* s%S \\ n'。$字符串10 $((padlength - $ {#字符串1} - $ {#字符串2})) $垫,$字符串2
I am writing a bash shell script to display if a process is running or not.
So far, I'am here.
printf "%-50s %s\n" $PROC_NAME [UP]
This gives me an output like:
JBoss [DOWN]
GlassFish [UP]
verylongprocessname [UP]
I want to pad the gap between the 2 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]
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.
pad=$(printf '%0.1s' "-"{1..60})
padlength=40
string2='bbbbbbb'
for string1 in a aa aaaa aaaaaaaa
do
printf '%s' "$string1"
printf '%*.*s' 0 $((padlength - ${#string1} - ${#string2} )) "$pad"
printf '%s\n' "$string2"
string2=${string2:1}
done
Unfortunately the length of the pad string has to be hardcoded, but the padlength can be a variable as shown.
Output:
a--------------------------------bbbbbbb
aa--------------------------------bbbbbb
aaaa-------------------------------bbbbb
aaaaaaaa----------------------------bbbb
Without subtracting the length of the second string:
a---------------------------------------bbbbbbb
aa--------------------------------------bbbbbb
aaaa------------------------------------bbbbb
aaaaaaaa--------------------------------bbbb
The first line could instead be the equivalent (similar to sprintf
):
printf -v pad '%0.1s' "-"{1..60}
You can do the printing all on one line if you prefer:
printf '%s%*.*s%s\n' "$string1" 0 $((padlength - ${#string1} - ${#string2} )) "$pad" "$string2"
这篇关于printf中填充字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!