* zmv pattern for directory and multiple file types
@ 2020-11-13 21:45 Ahmad Ismail
2020-11-16 19:53 ` Daniel Shahaf
0 siblings, 1 reply; 3+ messages in thread
From: Ahmad Ismail @ 2020-11-13 21:45 UTC (permalink / raw)
To: Zsh Users
[-- Attachment #1: Type: text/plain, Size: 1015 bytes --]
Hi All,
I have the following functions that rename files from camel to sort of
kebab case.
function ctokfiles() {
# Transform [Capital][Capital][Small] to [Capital]-[Capital][Small]
zmv -Q '(**/)(*[A-Z][A-Z][a-z]*)(.adoc|.txt)'
'$1${2//(#b)([A-Z])([A-Z][a-z])/$match[1]-$match[2]}$3'
# Put - Between [Small][Capital]
zmv -Q '(**/)(*[a-z][A-Z]*)(.adoc|.txt)'
'$1${2//(#b)([a-z])([A-Z])/$match[1]-$match[2]}$3'
# Change [Capital][Small] to Lovercase
zmv -Q '(**/)(*[A-Z][a-z]*)(.adoc|.txt)'
'$1${2//(#m)[A-Z][a-z]/${(L)MATCH}}$3'
}
So, IF the input name is "ThisIsMyOCDTalking", it becomes
"this-is-my-OCD-talking".
Now I want to rename directories, adoc & txt files only. For directories, I
think I have to use
zmv -Q '(**/)(*[A-Z][A-Z][a-z]*)(/)'
How to make a function that will work on adoc, txt and directories. I tried
(/|.adoc|.txt), apparently not working.
One more thing, is there any way I can reduce the steps in this functions.
*Thanks and Best Regards,Ahmad Ismail*
[-- Attachment #2: Type: text/html, Size: 2105 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: zmv pattern for directory and multiple file types
2020-11-13 21:45 zmv pattern for directory and multiple file types Ahmad Ismail
@ 2020-11-16 19:53 ` Daniel Shahaf
2020-11-16 23:08 ` Ahmad Ismail
0 siblings, 1 reply; 3+ messages in thread
From: Daniel Shahaf @ 2020-11-16 19:53 UTC (permalink / raw)
To: Ahmad Ismail; +Cc: Zsh Users
Ahmad Ismail wrote on Sat, 14 Nov 2020 03:45 +0600:
> Hi All,
>
> I have the following functions that rename files from camel to sort of
> kebab case.
>
> function ctokfiles() {
> # Transform [Capital][Capital][Small] to [Capital]-[Capital][Small]
> zmv -Q '(**/)(*[A-Z][A-Z][a-z]*)(.adoc|.txt)' '$1${2//(#b)([A-Z])([A-Z][a-z])/$match[1]-$match[2]}$3'
>
> # Put - Between [Small][Capital]
> zmv -Q '(**/)(*[a-z][A-Z]*)(.adoc|.txt)' '$1${2//(#b)([a-z])([A-Z])/$match[1]-$match[2]}$3'
>
> # Change [Capital][Small] to Lovercase
> zmv -Q '(**/)(*[A-Z][a-z]*)(.adoc|.txt)' '$1${2//(#m)[A-Z][a-z]/${(L)MATCH}}$3'
> }
>
> So, IF the input name is "ThisIsMyOCDTalking", it becomes
> "this-is-my-OCD-talking".
>
> Now I want to rename directories, adoc & txt files only. For directories, I
> think I have to use
>
> zmv -Q '(**/)(*[A-Z][A-Z][a-z]*)(/)'
>
> How to make a function that will work on adoc, txt and directories. I tried
> (/|.adoc|.txt), apparently not working.
>
Consider how in «*foo*(bar)», the «foo» are matched against the
filename but the «bar» are parsed as glob qualifiers. You can't just
juxtapose them within the same set of parentheses; they're apples and
oranges.
I don't see a good solution for "Everything in «**/*» that's
a directory or named *.foo or *.bar". I'd consider find(1) for this.
> One more thing, is there any way I can reduce the steps in this functions.
As a rule, I wouldn't recommend reducing steps in functions in the
first place, unless you're coding for sport rather than robustness — in
which case, I'd probably opt for Perl:
% print -rNC1 -- *.(adoc|txt)(N) *(/N) | rename -0 -n '$_ = join "-", map { /[a-z]/ ? lc : $_ } split /(?<=[a-z])(?=[A-Z])|(?=[A-Z][a-z])/'
rename(ThisIsMyOCDTalking.adoc, this-is-my-OCD-talking.adoc)
%
The main algorithmic difference is splitting the filename into an array
on zero-width lookaround matches. I don't think there's a native zsh
equivalent of this.
Note that changing «print -rNC1» to «ls» would introduce two separate bugs.
I'm not sure how to extend this to nested hierarchies, given the
possibility of foo/bar where both foo and bar have to be changed.
Perhaps with «rename -d» and ensuring the input is sorted children
before their parents.
Cheers,
Daniel
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: zmv pattern for directory and multiple file types
2020-11-16 19:53 ` Daniel Shahaf
@ 2020-11-16 23:08 ` Ahmad Ismail
0 siblings, 0 replies; 3+ messages in thread
From: Ahmad Ismail @ 2020-11-16 23:08 UTC (permalink / raw)
To: Daniel Shahaf; +Cc: Zsh Users
[-- Attachment #1: Type: text/plain, Size: 2666 bytes --]
Hi Daniel Shahaf,
Thank you very much.
*Thanks and Best Regards,Ahmad Ismail*
On Tue, Nov 17, 2020 at 1:53 AM Daniel Shahaf <d.s@daniel.shahaf.name>
wrote:
> Ahmad Ismail wrote on Sat, 14 Nov 2020 03:45 +0600:
> > Hi All,
> >
> > I have the following functions that rename files from camel to sort of
> > kebab case.
> >
> > function ctokfiles() {
> > # Transform [Capital][Capital][Small] to [Capital]-[Capital][Small]
> > zmv -Q '(**/)(*[A-Z][A-Z][a-z]*)(.adoc|.txt)'
> '$1${2//(#b)([A-Z])([A-Z][a-z])/$match[1]-$match[2]}$3'
> >
> > # Put - Between [Small][Capital]
> > zmv -Q '(**/)(*[a-z][A-Z]*)(.adoc|.txt)'
> '$1${2//(#b)([a-z])([A-Z])/$match[1]-$match[2]}$3'
> >
> > # Change [Capital][Small] to Lovercase
> > zmv -Q '(**/)(*[A-Z][a-z]*)(.adoc|.txt)'
> '$1${2//(#m)[A-Z][a-z]/${(L)MATCH}}$3'
> > }
> >
> > So, IF the input name is "ThisIsMyOCDTalking", it becomes
> > "this-is-my-OCD-talking".
> >
> > Now I want to rename directories, adoc & txt files only. For
> directories, I
> > think I have to use
> >
> > zmv -Q '(**/)(*[A-Z][A-Z][a-z]*)(/)'
> >
> > How to make a function that will work on adoc, txt and directories. I
> tried
> > (/|.adoc|.txt), apparently not working.
> >
>
> Consider how in «*foo*(bar)», the «foo» are matched against the
> filename but the «bar» are parsed as glob qualifiers. You can't just
> juxtapose them within the same set of parentheses; they're apples and
> oranges.
>
> I don't see a good solution for "Everything in «**/*» that's
> a directory or named *.foo or *.bar". I'd consider find(1) for this.
>
> > One more thing, is there any way I can reduce the steps in this
> functions.
>
> As a rule, I wouldn't recommend reducing steps in functions in the
> first place, unless you're coding for sport rather than robustness — in
> which case, I'd probably opt for Perl:
>
> % print -rNC1 -- *.(adoc|txt)(N) *(/N) | rename -0 -n '$_ = join "-", map
> { /[a-z]/ ? lc : $_ } split /(?<=[a-z])(?=[A-Z])|(?=[A-Z][a-z])/'
> rename(ThisIsMyOCDTalking.adoc, this-is-my-OCD-talking.adoc)
> %
>
> The main algorithmic difference is splitting the filename into an array
> on zero-width lookaround matches. I don't think there's a native zsh
> equivalent of this.
>
> Note that changing «print -rNC1» to «ls» would introduce two separate bugs.
>
> I'm not sure how to extend this to nested hierarchies, given the
> possibility of foo/bar where both foo and bar have to be changed.
> Perhaps with «rename -d» and ensuring the input is sorted children
> before their parents.
>
> Cheers,
>
> Daniel
>
[-- Attachment #2: Type: text/html, Size: 4121 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-11-16 23:09 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-13 21:45 zmv pattern for directory and multiple file types Ahmad Ismail
2020-11-16 19:53 ` Daniel Shahaf
2020-11-16 23:08 ` Ahmad Ismail
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).