在 bash 中,如何添加带前导零的整数并维护指定的缓冲区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3191067/
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-09 19:25:47  来源:igfitidea点击:

In bash, how could I add integers with leading zeroes and maintain a specified buffer

bashshelloutput-formatting

提问by readdit

For example, I want to count from 001 to 100. Meaning the zero buffer would start off with 2, 1, then eventually 0 when it reaches 100 or more.

例如,我想从 001 数到 100。这意味着零缓冲区将从 2、1 开始,然后在达到 100 或更多时最终为 0。

ex: 001 002 ... 010 011 ... 098 099 100

例如:001 002 ... 010 011 ... 098 099 100

I could do this if the numbers had a predefined number of zeroes with printf "%02d" $i. But that's static and not dynamic and would not work in my example.

如果数字具有 printf "%02d" $i 的预定义数量的零,我可以这样做。但这是静态的而不是动态的,在我的示例中不起作用。

回答by Paused until further notice.

If by static versus dynamic you mean that you'd like to be able to use a variable for the width, you can do this:

如果通过静态与动态,您的意思是您希望能够使用宽度变量,您可以这样做:

$ padtowidth=3
$ for i in 0 {8..11} {98..101}; do printf "%0*d\n" $padtowidth $i; done
000
008
009
010
011
098
099
100
101

The asterisk is replaced by the value of the variable it corresponds to in the argument list ($padtowidthin this case).

星号被替换为它在参数列表中对应的变量的值($padtowidth在本例中)。

Otherwise, the only reason your example doesn't work is that you use "2" (perhaps as if it were the maximum padding to apply) when it should be "3" (as in my example) since that value is the resulting total width (not the pad-only width).

否则,您的示例不起作用的唯一原因是您使用了“2”(可能好像它是要应用的最大填充),而它应该是“3”(如我的示例),因为该值是结果总数宽度(不是仅焊盘宽度)。

回答by mob

If your system has it, try seqwith the -w(--equal-width) option:

如果您的系统有它,请尝试seq使用-w( --equal-width) 选项:

$ seq -s, -w 1 10
01,02,03,04,05,06,07,08,09,10

$ for i in `seq -w 95 105` ; do echo -n " $i" ; done
095 096 097 098 099 100 101 102 103 104 105

回答by Johannes Schaub - litb

In Bash version 4 (use bash -version) you can use brace expansion. Putting a 0before either limit forces the numbers to be padded by zeros

在 Bash 版本 4(使用 bash -version)中,您可以使用大括号扩展0在任一限制之前放置 a强制数字用零填充

echo {01..100} # 001 002 003 ...
echo {03..100..3} # 003 006 009 ...

回答by SiegeX

#!/bin/bash

max=100; 

for ((i=1;i<=$max;i++)); do 
    printf "%0*d\n" ${#max} $i
done

The code above will auto-pad your numbers with the correct number of 0's based upon how many digits the max/terminal value contains. All you need to do is change the maxvariable and it will handle the rest.

上面的代码将根据最大/终端值包含的位数自动用正确数量的 0 填充您的数字。您需要做的就是更改max变量,它会处理其余的。

Examples:

例子:

max=10

最大值=10

01
02
03
04
05
06
07
08
09
10

max=100

最大值=100

001
002
003
004
005
006
...
097
098
099
100

max=1000

最大值=1000

0001
0002
0003
0004
0005
0006
...
0997
0998
0999
1000

回答by juan

# jot is available on FreeBSD, Mac OS X, ...    
jot -s " " -w '%03d' 5   
jot -s " " -w '%03d' 10  
jot -s " " -w '%03d' 50  
jot -s " " -w '%03d' 100   

回答by khaverim

If you need to pad values up to a variable number with variable padding:

如果您需要使用可变填充将值填充到可变数字:

$values_count=514;
$padding_width=5;

for i in 0 `seq 1 $(($values_count - 1))`; do printf "%0*d\n" $padding_width $i; done;

This would print out 00000, 00001, ... 00513. (I didn't find any of the current answers meeting my need)

这将打印出 00000, 00001, ... 00513。(我没有找到任何满足我需要的当前答案)