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

Re: can I control brace expansion's "sort"/"display" order?



2008/8/31 Eric De Mund <ead@xxxxxxxxx>:
> Hello,
>
> Can I control brace expansion's "sort" or "display" order?
>
> For instance this zsh echo results in this output:
>
>    % echo {white,black}{bird,fish}{21..24} | tr ' ' '\n'
>    # current output order
>    whitebird21
>    whitebird22
>    whitebird23
>    whitebird24
>    whitefish21
>    whitefish22
>    whitefish23
>    whitefish24
>    blackbird21
>    blackbird22
>    blackbird23
>    blackbird24
>    blackfish21
>    blackfish22
>    blackfish23
>    blackfish24
>    %
>
> But what if instead of this order I wanted the last brace to cycle most
> slowly, followed by the first, followed by the second, like so:
>
>    % echo {white,black}{bird,fish}{21..24} | tr ' ' '\n'
>    # desired output order
>    whitebird21
>    whitefish21
>    blackbird21
>    blackfish21
>    whitebird22
>    blackbird22
>    whitefish22
>    blackfish22
>    whitebird23
>    whitefish23
>    blackbird23
>    blackfish23
>    whitebird24
>    whitefish24
>    blackbird24
>    blackfish24
>    %
>
> Is there any way to accomplish this?
>
> My problem is that I'm trying to get files added to a zip archive in a
> particular order, and this order is neither brace expansion sort/display
> order nor lexicographical order.

You can probably do it with some for-looping, like
setopt shortloops
% for a in white black; for b in fish bird; for c in {21..24}; echo $a$b$c
whitefish21
whitefish22
whitefish23
whitefish24
whitebird21
whitebird22
whitebird23
whitebird24
blackfish21
blackfish22
blackfish23
blackfish24
blackbird21
blackbird22
blackbird23
blackbird24

Now you can just swap the order of the loops
% for b in fish bird; for a in white black; for c in {21..24}; echo $a$b$c
whitefish21
whitefish22
whitefish23
whitefish24
blackfish21
blackfish22
blackfish23
blackfish24
whitebird21
whitebird22
whitebird23
whitebird24
blackbird21
blackbird22
blackbird23
blackbird24

-- 
Mikael Magnusson



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