In zsh, when I have to create a bunch of files with zsh, I usually do something like:
for x in $(seq 1 1000); do .....; done
This works fine, it gives me files with names foo-1.txt
.. foo-1000.txt
.
However, these files do not sort nicely, so I would like to zero-pad the $x
variable, thus producing names of foo-0001.txt
.. foo-1000.txt
.
How to do that in zsh? (and bonus question, how to do it in bash?)
The character ^ indicates beginning of a line; 0* means any number of zeroes including none. This essentially returns you the number with all 0s, if any, at the beginning removed.
Zero padding is a technique typically employed to make the size of the input sequence equal to a power of two. In zero padding, you add zeros to the end of the input sequence so that the total number of samples is equal to the next higher power of two.
For reference sake, if you do not have control over the generation of the numeric values, you can do padding with:
% value=314 % echo ${(l:10::0:)value} 0000000314 % echo $value 314
Use the -w
flag to seq
(in any shell):
$ seq -w 1 10 01 02 03 04 05 06 07 08 09 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With