How to increment a zero padded int in Bash
I have a set of records to loop. The numbers range f开发者_StackOverflowrom 0000001 to 0089543 that ill call UIDX.
if i try something like:
for ((i=0; i< 0089543; i++)); do
((UIDX++))
done
counter increments 1, 2, 3, 4 as opposed to the 0000001, 0000002... that i need.
what is the best way to pad those leading zero's?
Use the printf
command to format the numbers with leading zeroes, eg:
for ((i = 0; i < 99; ++i)); do printf -v num '%07d' $i; echo $num; done
From man bash
:
printf [-v var] format [arguments]
Write the formatted arguments to the standard output under the control of the format. The -v option causes the output to be assigned to the variable var rather than being printed to the standard output.
Bash 4 has a nice way to solve this:
for i in {0000000..0089543}; do
echo $i
done
You could use the seq command, very useful in your situation
seq -w 0089543
Remove the first and last number according to your need, for example, if you need to arrive to 0089542 then the command to use is
seq -w 0089542
精彩评论