in reply to String expansion script

This is great. I especially like the distinction between [01:20] and [1:20].

The zsh shell has a brace expansion function, so you can write this in zsh:

echo f-{fred,barney,vilma}/h{08..22}.t
which prints
f-fred/h08.t f-fred/h09.t f-fred/h10.t f-fred/h11.t f-fred/h12.t f-fre +d/h13.t f-fred/h14.t f-fred/h15.t f-fred/h16.t f-fred/h17.t f-fred/h1 +8.t f-fred/h19.t f-fred/h20.t f-fred/h21.t f-fred/h22.t f-barney/h08. +t f-barney/h09.t f-barney/h10.t f-barney/h11.t f-barney/h12.t f-barne +y/h13.t f-barney/h14.t f-barney/h15.t f-barney/h16.t f-barney/h17.t f +-barney/h18.t f-barney/h19.t f-barney/h20.t f-barney/h21.t f-barney/h +22.t f-vilma/h08.t f-vilma/h09.t f-vilma/h10.t f-vilma/h11.t f-vilma/ +h12.t f-vilma/h13.t f-vilma/h14.t f-vilma/h15.t f-vilma/h16.t f-vilma +/h17.t f-vilma/h18.t f-vilma/h19.t f-vilma/h20.t f-vilma/h21.t f-vilm +a/h22.t
Zsh makes the distinction between {08..22} and {8..22}. Bash 3.0 has cloned this numeric brace expansion feature too (it had non-numeric brace expansion originally), but it expands {08..11} as 8 9 10 11. (Brace expansion with multiple braces sometimes expand in a different order in the shells and your program.)

By the way, your program does not fully expand m[2:5]-{a:t:g:c}, it just prints the four strings m[2:5]-a m[2:5]-t m[2:5]-g m[2:5]-c. Is that intentional?

Also, your program does not seem to be able to expand braces embedded in each other, such as {/usr{:/local}:/var}/lib. Bash expands this to /usr/lib /usr/local/lib /var/lib.

Replies are listed 'Best First'.
Re^2: String expansion script
by jdporter (Paladin) on Feb 03, 2005 at 20:23 UTC

    I for one would prefer not to see this idea made specific to pathname matching. I see it more as a kind of narrow-case templating. Comparing it to capabilities currently available in shells may not be fair or desirable; for example, the brace expansion features of zsh/bash result only in the names of existing paths that match; so if you say ls m{8:11} but have no files matching that pattern, the result is null, not m8 m9 m10 m11.

    As for nesting - I think calling the expander reiteratively (or recursively ;-) could give you that.

      Brace expansion is not specific to pathname matching. I've shown you: type

      echo a{1..3}
      you get
      a1 a2 a3
      even if such files do not exist.
      echo > a1; ls a{1..3}
      prints
      ls: a2: No such file or directory ls: a3: No such file or directory a1

      There is a filename matching feature too, in bash it looks like this:

      shopt -s extglob echo a@(1|2|3)
      is really similar to the friendly echo a[1-3], it prints
      a1
      if a1 exists but the other files don't. (Don't ask me how it works in zsh.) There is no numeric range variant of this though.
        Yah, I know. That occurred to me after I posted. /me hasn't had his cup of coffee yet.