zsh-users
 help / color / mirror / code / Atom feed
* translate all sequences of whitespace/underscores to single dashes
@ 2015-08-17  0:20 Emanuel Berg
  2015-08-17  1:57 ` Kurtis Rader
  0 siblings, 1 reply; 6+ messages in thread
From: Emanuel Berg @ 2015-08-17  0:20 UTC (permalink / raw)
  To: zsh-users

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 "-"`

Here is the file for context:

    http://user.it.uu.se/~embe8573/conf/.zsh/files-fs

Feel free to help me with other issues in that file
as well.

-- 
underground experts united
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: translate all sequences of whitespace/underscores to single dashes
  2015-08-17  0:20 translate all sequences of whitespace/underscores to single dashes Emanuel Berg
@ 2015-08-17  1:57 ` Kurtis Rader
  2015-08-17  8:47   ` Peter Stephenson
  0 siblings, 1 reply; 6+ messages in thread
From: Kurtis Rader @ 2015-08-17  1:57 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 602 bytes --]

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


-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: translate all sequences of whitespace/underscores to single dashes
  2015-08-17  1:57 ` Kurtis Rader
@ 2015-08-17  8:47   ` Peter Stephenson
  2015-08-17 11:27     ` Jun T.
       [not found]     ` <9D99B267-5DDD-4C5B-A33A-C5A0251A4B9F__19706.0165032328$1439813299$gmane$org@kba.biglobe.ne.jp>
  0 siblings, 2 replies; 6+ messages in thread
From: Peter Stephenson @ 2015-08-17  8:47 UTC (permalink / raw)
  To: Zsh Users

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: translate all sequences of whitespace/underscores to single dashes
  2015-08-17  8:47   ` Peter Stephenson
@ 2015-08-17 11:27     ` Jun T.
       [not found]     ` <9D99B267-5DDD-4C5B-A33A-C5A0251A4B9F__19706.0165032328$1439813299$gmane$org@kba.biglobe.ne.jp>
  1 sibling, 0 replies; 6+ messages in thread
From: Jun T. @ 2015-08-17 11:27 UTC (permalink / raw)
  To: zsh-users

In theory, if we want the same effect as the original code
(which uses 'tr -s "-"'), we need

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

so that x='a_-_z' becomes 'a-z' (not 'a---z').

BTW, the line
	mv $f $new_name 2> /dev/null
in the function clean-filename() is dangerous, because two files
$f can have the same $new_name, in which case one of them is lost
(or $new_name may already exist before the function is called).

# If http://user.it.uu.se/~embe8573/conf/.zsh/files-fs is just a
# simplified version and you already have a correct handling of
# the problem then please forget this note.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: translate all sequences of whitespace/underscores to single dashes
       [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
  0 siblings, 1 reply; 6+ messages in thread
From: Stephane Chazelas @ 2015-08-17 12:19 UTC (permalink / raw)
  To: zsh-users

2015-08-17 20:27:22 +0900, Jun T.:
> In theory, if we want the same effect as the original code
> (which uses 'tr -s "-"'), we need
> 
> 	${x//[-_[:space:]]##/-}
> 
> so that x='a_-_z' becomes 'a-z' (not 'a---z').
> 
> BTW, the line
> 	mv $f $new_name 2> /dev/null
> in the function clean-filename() is dangerous, because two files
> $f can have the same $new_name, in which case one of them is lost
> (or $new_name may already exist before the function is called).
[...]

Note that zmv accounts for that (and also adds the missing
"--") (and turns on extendedglob).

zmv '*' '${f//[-_[:space:]]##/-}'

-- 
Stephane


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: translate all sequences of whitespace/underscores to single dashes
  2015-08-17 12:19       ` Stephane Chazelas
@ 2015-08-17 14:51         ` Bart Schaefer
  0 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2015-08-17 14:51 UTC (permalink / raw)
  To: zsh-users

On Aug 17,  1:19pm, Stephane Chazelas wrote:
} Subject: Re: translate all sequences of whitespace/underscores to single d
}
} 2015-08-17 20:27:22 +0900, Jun T.:
} > BTW, the line
} > 	mv $f $new_name 2> /dev/null
} > in the function clean-filename() is dangerous
} 
} Note that zmv accounts for that (and also adds the missing
} "--") (and turns on extendedglob).
} 
} zmv '*' '${f//[-_[:space:]]##/-}'

Two additional points:

(1) The $f in Jun's warning has nothing to do with the the $f in 
    Stephane's example except that they coincidentally both were
    used as a variable holding a file name.

(2) That use of $f in zmv is explained in the comments in the zmv
    source file, but isn't mentioned in the man pages / info docs.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-08-17 14:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-17  0:20 translate all sequences of whitespace/underscores to single dashes Emanuel Berg
2015-08-17  1:57 ` Kurtis Rader
2015-08-17  8:47   ` Peter Stephenson
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

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).