bash 如何在bash中对整数序列进行零填充,以便所有整数都具有相同的宽度?

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

How to zero pad a sequence of integers in bash so that all have the same width?

bashnumberspadding

提问by John Marston

I need to loop some values,

我需要循环一些值,

for i in $(seq $first $last)
do
    does something here
done

For $firstand $last, i need it to be of fixed length 5. So if the input is 1, i need to add zeros in front such that it becomes 00001. It loops till 99999for example, but the length has to be 5.

对于$firstand $last,我需要它的固定长度为 5。所以如果输入是1,我需要在前面添加零,使其变为0000199999例如,它循环直到,但长度必须为 5。

E.g.: 00002, 00042, 00212, 012312and so forth.

如:000020004200212012312等等。

Any idea on how i can do that?

关于我如何做到这一点的任何想法?

回答by Dave Webb

In your specific case though it's probably easiest to use the -fflag to seqto get it to format the numbers as it outputs the list. For example:

在您的特定情况下,尽管使用该-f标志可能最容易使其在seq输出列表时格式化数字。例如:

for i in $(seq -f "%05g" 10 15)
do
  echo $i
done

will produce the following output:

将产生以下输出:

00010
00011
00012
00013
00014
00015

More generally, bashhas printfas a built-in so you can pad output with zeroes as follows:

更一般地,bash具有printf作为内置,因此您可以使用零填充输出,如下所示:

$ i=99
$ printf "%05d\n" $i
00099

You can use the -vflag to store the output in another variable:

您可以使用该-v标志将输出存储在另一个变量中:

$ i=99
$ printf -v j "%05d" $i
$ echo $j
00099

Notice that printfsupports a slightly different format to seqso you need to use %05dinstead of %05g.

请注意,printf支持一个稍微不同的格式seq,所以你需要使用%05d的替代%05g

回答by Indie

Easier still you can just do

更简单,你可以做

for i in {00001..99999}; do
  echo $i
done

回答by m_messiah

If the end of sequence has maximal length of padding (for example, if you want 5 digits and command is "seq 1 10000"), than you can use "-w" flag for seq - it adds padding itself.

如果序列的末尾具有最大的填充长度(例如,如果您想要 5 位数字并且命令是“seq 1 10000”),那么您可以对 seq 使用“-w”标志 - 它会自行添加填充。

seq -w 1 10

produce

生产

01
02
03
04
05
06
07
08
09
10

回答by frankc

use printf with "%05d" e.g.

将 printf 与“%05d”一起使用,例如

printf "%05d" 1

回答by jaypal singh

Very simple using printf

使用非常简单 printf

[jaypal:~/Temp] printf "%05d\n" 1
00001
[jaypal:~/Temp] printf "%05d\n" 2
00002

回答by anubhava

Use awk like this:

像这样使用 awk:

awk -v start=1 -v end=10 'BEGIN{for (i=start; i<=end; i++) printf("%05d\n", i)}'

OUTPUT:

输出:

00001
00002
00003
00004
00005
00006
00007
00008
00009
00010

Update:

更新:

As pure bash alternative you can do this to get same output:

作为纯 bash 替代方案,您可以这样做以获得相同的输出:

for i in {1..10}
do
   printf "%05d\n" $i
done

This way you can avoidusing an external program seqwhich is NOT availableon all the flavors of *nix.

这种方式可以避开使用外部程序seq不可用* nix上的所有的味道。

回答by unifex

I pad output with more digits (zeros) than I need then use tail to only use the number of digits I am looking for. Notice that you have to use '6' in tail to get the last five digits :)

我用比我需要的更多的数字(零)填充输出,然后使用 tail 仅使用我正在寻找的数字数量。请注意,您必须在尾部使用 '6' 来获取最后五位数字 :)

for i in $(seq 1 10)
do
RESULT=$(echo 00000$i | tail -c 6)
echo $RESULT
done

回答by Burghard Hoffmann

If you want N digits, add 10^N and delete the first digit.

如果你想要 N 个数字,添加 10^N 并删除第一个数字。

for (( num=100; num<=105; num++ ))
do
  echo ${num:1:3}
done

Output:

输出:

01
02
03
04
05

回答by Chris

This will work also:

这也将起作用:

for i in {0..9}{0..9}{0..9}{0..9}
do
  echo "$i"
done

回答by Paco

Other way :

另一种方式 :

zeroos="000"
echo 

for num in {99..105};do
 echo ${zeroos:${#num}:${#zeroos}}${num}
done

So simple function to convert any number would be:

转换任何数字的简单函数是:

function leading_zero(){

    local num=
    local zeroos=00000
    echo ${zeroos:${#num}:${#zeroos}}${num} 

}