zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.stephenson@samsung.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: translate all sequences of whitespace/underscores to single dashes
Date: Mon, 17 Aug 2015 09:47:52 +0100	[thread overview]
Message-ID: <20150817094752.53c314fe@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <CABx2=D_ji1dy1K3UxxD5vKyjJ+pckLMqj1Pr4Si8i3G2c30mXA@mail.gmail.com>

On Sun, 16 Aug 2015 18:57:02 -0700
Kurtis Rader <krader@skepticism.us> wrote:
> On Sun, Aug 16, 2015 at 5:20 PM, Emanuel Berg <embe8573@student.uu.se>
> 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


  reply	other threads:[~2015-08-17  8:48 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-17  0:20 Emanuel Berg
2015-08-17  1:57 ` Kurtis Rader
2015-08-17  8:47   ` Peter Stephenson [this message]
2015-08-17 11:27     ` Jun T.
     [not found]     ` <9D99B267-5DDD-4C5B-A33A-C5A0251A4B9F__19706.0165032328$1439813299$gmane$org@kba.biglobe.ne.jp>
2015-08-17 12:19       ` Stephane Chazelas
2015-08-17 14:51         ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150817094752.53c314fe@pwslap01u.europe.root.pri \
    --to=p.stephenson@samsung.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).