Vra

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

Was dit nuttig?

Oplossing

Use the -w flag to seq (in any shell):

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

Ander wenke

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

You can use bash's brace expansion:

$ for n in file-{0001..1000}.txt; do echo $n; done
file-0001.txt
file-0002.txt
file-0003.txt
file-0004.txt
file-0005.txt
file-0006.txt
file-0007.txt
file-0008.txt
file-0009.txt
file-0010.txt
...
file-0998.txt
file-0999.txt
file-1000.txt

Works in zsh too.

Works in bash (don't know for zsh):

echo foo{0000-1000}.txt 
  1. printf
    Using printf to pad with zeros works in any shell:

    $ printf 'foo%03d.txt\n' $(seq 4)
    foo001.txt
    foo002.txt
    foo003.txt
    foo004.txt
    
    $           printf  'foo%03d.txt\n' {1..4}       # works in bash, ksh, zsh.
    $ n=4;      printf  'foo%03d.txt\n' {1..$n}      # only ksh, zsh
    $ n=4; eval printf '"foo%03d.txt\n" {1..'"$n"'}' # workaround needed for bash
    
  2. Use brace expansion (only bash and zsh keep the leading zeros ksh don't).

    $ printf '%s\n' foo{1..004}.txt
    foo001.txt
    foo002.txt
    foo003.txt
    foo004.txt
    
Gelisensieer onder: CC-BY-SA met toeskrywing
Nie verbonde aan StackOverflow
scroll top