在 linux 中格式化 bash - printf 对齐中间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20361698/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:50:56 来源:igfitidea点击:
formatting bash in linux - printf align middle
提问by andrej
This line of code:
这行代码:
printf 'ddd %-22s dddd \n' "eeeeeee"
Aligns to the left.
向左对齐。
What could I use to align it to centre like this:
我可以用什么来将它对齐到这样的中心:
ddd eeeeeee dddd
采纳答案by akrog
A bit tricky ... but what about this? ;)
有点棘手……但是这个呢?;)
STR="eeeeeee"; printf 'ddd %11s%-11s dddd \n' `echo $STR | cut -c 1-$((${#STR}/2))` `echo $STR | cut -c $((${#STR}/2+1))-${#STR}`
回答by PasteBT
printf not support it, but easy to implement it:
printf 不支持,但易于实现:
D="12" # input string
BS=10 # buffer size
L=$(((BS-${#D})/2))
[ $L -lt 0 ] && L=0
printf "start %${L}s%s%${L}s end\n" "" $D ""
回答by armandino
Just add some tabs around it?
只是在它周围添加一些标签?
printf "ddd\t\teeeeee\t\tddd"