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

Re: translate all sequences of whitespace/underscores to single dashes



On Sun, 16 Aug 2015 18:57:02 -0700
Kurtis Rader <krader@xxxxxxxxxxxxx> wrote:
> On Sun, Aug 16, 2015 at 5:20 PM, Emanuel Berg <embe8573@xxxxxxxxxxxxx>
> wrote:
> 
> > How can I do this directly and only in zsh, i.e.
> > not using tr (translate characters)?
> >
> >     # whitespace(s) and underscore(s) -> (a single) dash
> >     new_name=${new_name//[_ ]/-}
> >     new_name=`echo $new_name | tr -s "-"`
> >
> 
> The patterns are the same as those for filename generation (so see that
> section of "man zshexpn"). The solution is to enable extended_glob support.
> 
> # setopt extendedglob
> # x='a b_c_  d'
> # print ${x//[_ ]##/-}
> a-b-c-d

The question also implies white spcae, not just single spaces --- so I'd
also suggest

${x//[_[:space:]]##/-}

pws



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