bash - 在bash中右对齐/填充数字

在 bash 中打印输出时填充数字的最佳方法是什么,以便数字在屏幕上右对齐。所以这个:

00364.txt with 28 words in 0m0.927s
00366.txt with 105 words in 0m2.422s
00367.txt with 168 words in 0m3.292s
00368.txt with 1515 words in 0m27.238

应该这样打印:

00364.txt with   28 words in 0m0.927s
00366.txt with  105 words in 0m2.422s
00367.txt with  168 words in 0m3.292s
00368.txt with 1515 words in 0m27.238

我在 for 循环中逐行打印这些内容。而且我会知道文件中字数的上限(只是现在还不行)。

最佳答案

对于 bash,使用带有对齐标志的 printf 命令。

例如:

printf '..%7s..' 'hello'

打印:

..  hello..

现在,请自行决定解决问题。

https://stackoverflow.com/questions/994461/

相关文章:

Java - 如何将数字格式化为 2 char 字符串?

android - 在 Android 的 textview [ Html.fromhtml() ]

angularjs - 使用 AngularJS 指令格式化输入字段,同时保持范围变量不变

c# - 用年份格式化 TimeSpan

css - 如何在 Zurb Foundation 中垂直居中元素?

android - 在 android textview 中显示