Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: translate all sequences of whitespace/underscores to single dashes
- X-seq: zsh-users 20423
- From: Peter Stephenson <p.stephenson@xxxxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: translate all sequences of whitespace/underscores to single dashes
- Date: Mon, 17 Aug 2015 09:47:52 +0100
- In-reply-to: <CABx2=D_ji1dy1K3UxxD5vKyjJ+pckLMqj1Pr4Si8i3G2c30mXA@mail.gmail.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- Organization: Samsung Cambridge Solution Centre
- References: <87r3n2dav7.fsf@nl106-137-147.student.uu.se> <CABx2=D_ji1dy1K3UxxD5vKyjJ+pckLMqj1Pr4Si8i3G2c30mXA@mail.gmail.com>
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