How to zero pad a sequence of integers in bash so

2019-01-01 12:08发布

问题:

I need to loop some values,

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

For $first and $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 99999 for example, but the length has to be 5.

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

Any idea on how i can do that?

回答1:

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

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, bash has printf as a built-in so you can pad output with zeroes as follows:

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

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

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

Notice that printf supports a slightly different format to seq so you need to use %05d instead of %05g.



回答2:

Easier still you can just do

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


回答3:

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.

seq -w 1 10

produce

01
02
03
04
05
06
07
08
09
10


回答4:

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

printf \"%05d\" 1


回答5:

Very simple using printf

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


回答6:

Use awk like this:

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:

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

This way you can avoid using an external program seq which is NOT available on all the flavors of *nix.



回答7:

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 :)

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


回答8:

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

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

Output:

01
02
03
04
05


回答9:

This will work also:

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


回答10:

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=$1
    local zeroos=00000
    echo ${zeroos:${#num}:${#zeroos}}${num} 

}