Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: pad numbers



On 2008-11-15 at 12:36 -0600, fREW Schmidt wrote:
> Ah shoot, I forgot to mention that the files are actually named like this
> 01 - foo bar.baz

If you want to specify the length more manually than the simple method:

% mkdir T && cd T
% touch '01 - Foo' '02 - Bar' '03 - Baz' '100 - Fred' '2000 - Long'

naive:
% zmv '([[:digit:]]##)(*)' '${(l:3::0:)1}$2'
% ls
000 - Long 001 - Foo  002 - Bar  003 - Baz  100 - Fred

Note that this has truncated the 2000 entry because I have explicitly
specified that $1 should be fit within a field three characters wide,
left-padded with digit 0.  See zshexpn(1), Parameter Expansion Flags.

Recreating the original files, then:
% zmv '([[:digit:]]|[[:digit:]][[:digit:]])([^[:digit:]]*)' '${(l:3::0:)1}$2'
% ls
001 - Foo   002 - Bar   003 - Baz   100 - Fred  2000 - Long

You might prefer [0-9] to [[:digit:]].

Perhaps you prefer the zmv -W approach.  ;-)

-Phil



Messages sorted by: Reverse Date, Date, Thread, Author