LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-11-2020, 09:52 AM   #1
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 91

Rep: Reputation: 0
zsh - zmv - do not change case if there are more than one consecutive uppercase letters


I am using the following command

Code:
zmv -n -Q '(**/)(*[[:upper:]]*)(/)' '${1}${(L)${2//(#b)([[:upper:]])/-$match[1]}#-}'
to transform

Code:
    % tree
    .
    ├── EmptyFile.txt
    ├── FirstDirectoryName
    │   ├── FourthDirectoryName
    │   ├── secondDirectoryName
    │   └── thirdDirectoryName
    ├── FourthDirectoryName
    ├── secondDirectoryName
    └── thirdDirectoryName
To

Code:
    % tree
    .
    ├── EmptyFile.txt
    ├── first-directory-name
    │   ├── fourth-directory-name
    │   ├── second-directory-name
    │   └── third-directory-name
    ├── fourth-directory-name
    ├── second-directory-name
    └── third-directory-name
However there is a small problem.

I do not want to lowercase if there are more than one consecutive uppercase letters. I just want to put `-` behind the capital letters given it is not in the beginning.

Code:
    % tree
    .
    ├── DDDDDDD
    ├── FirstFolderToRename
    │   ├── DDDDDDD
    │   └── ThisIsDDDDD
    ├── secondFolderToRename
    │   ├── DDDDDDD
    │   └── ThisIsDDDDD
    └── ThisIsDDDDD
Current Output

Code:
    % zmv -n -Q '(**/)(*[[:upper:]]*)(/)' '${1}${(L)${2//(#b)([[:upper:]])/-$match[1]}#-}'
    mv -- FirstFolderToRename/DDDDDDD FirstFolderToRename/d-d-d-d-d-d-d
    mv -- FirstFolderToRename/ThisIsDDDDD FirstFolderToRename/this-is-d-d-d-d-d
    mv -- secondFolderToRename/DDDDDDD secondFolderToRename/d-d-d-d-d-d-d
    mv -- secondFolderToRename/ThisIsDDDDD secondFolderToRename/this-is-d-d-d-d-d
    mv -- DDDDDDD d-d-d-d-d-d-d
    mv -- FirstFolderToRename first-folder-to-rename
    mv -- secondFolderToRename second-folder-to-rename
    mv -- ThisIsDDDDD this-is-d-d-d-d-d
Expected Output

Code:
    mv -- FirstFolderToRename/DDDDDDD FirstFolderToRename/DDDDDDD
    mv -- FirstFolderToRename/ThisIsDDDDD FirstFolderToRename/this-is-DDDDD
    mv -- secondFolderToRename/DDDDDDD secondFolderToRename/DDDDDDD
    mv -- secondFolderToRename/ThisIsDDDDD secondFolderToRename/this-is-DDDDD
    mv -- DDDDDDD DDDDDDD
    mv -- FirstFolderToRename first-folder-to-rename
    mv -- secondFolderToRename second-folder-to-rename
    mv -- ThisIsDDDDD this-is-DDDDD
 
Old 07-11-2020, 10:41 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 1,140

Rep: Reputation: Disabled
Try to replace [[:upper:]] with [[:upper:]][[:lower:]] and (#b) with (#m):
Code:
'${1}${${2//(#m)[[:upper:]][[:lower:]]/-${(L)MATCH}}#-}'
This won't put the dash before DDDDD though.

Consider doing this in two steps then. First, put dashes between words:
Code:
zmv -n -Q '(**/)(*[A-Z]*)(/)' '$1${2//(#b)([a-z])([A-Z])/$match[1]-$match[2]}'
Then, change the case:
Code:
zmv -n Q '(**/)(*[A-Z][a-z]*)(/)' '$1${2//(#m)[A-Z][a-z]/${(L)MATCH}}'

Last edited by shruggy; 07-11-2020 at 04:30 PM.
 
1 members found this post helpful.
Old 07-11-2020, 10:36 PM   #3
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 91

Original Poster
Rep: Reputation: 0
Thank you very much. I have made a function like

Code:
function CamelToKebab() {

    zmv -Q '(**/)(*[A-Z]*)(/)' '$1${2//(#b)([a-z])([A-Z])/$match[1]-$match[2]}'
    zmv -Q '(**/)(*[A-Z][a-z]*)(/)' '$1${2//(#m)[A-Z][a-z]/${(L)MATCH}}'

}
Can you please show me how to go back to pascal case? I mean I am looking for

Code:
function KebabToPascel() {
 ....
}

Last edited by blueray; 07-11-2020 at 10:44 PM.
 
Old 07-12-2020, 01:08 AM   #4
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 91

Original Poster
Rep: Reputation: 0
I think the process should be simple

1. Capitalize First Character
2. Capitalize Every Character After `-`
3. Remove all `-`

So far I have figured out 1 & 3.

Code:
function KebabToPascal () {
    zmv -n -Q '(**/)(*-*)(/)' '$1${(U)2:0:1}${2#?}'
    // Capitalize Every Character After `-`
    zmv -n -Q '(**/)(*-[a-z]*)(/)' '$1${2//(-##)/}'
}
So the question boils down to How To Capitalize Every Character After `-`

Last edited by blueray; 07-12-2020 at 01:43 AM.
 
Old 07-12-2020, 02:15 AM   #5
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 91

Original Poster
Rep: Reputation: 0
Alhamdulillah. The function that is working is

Code:
function kebabToPascal () {
    zmv -Q '(**/)(*-*)(/)' '$1${(U)2:0:1}${2#?}'
    zmv -Q '(**/)(*-[a-z]*)(/)' '$1${2//(#m)-[a-z]/${(U)MATCH}}'
    zmv -Q '(**/)(*-*)(/)' '$1${2//-##/}'
}
 
  


Reply

Tags
bash, zsh


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Not Understanding zmv Command blueray Linux - Newbie 2 07-13-2020 01:58 AM
ZSH -- Apple switching BASH => ZSH, what do you think? LQParsons Linux - Newbie 2 06-06-2019 10:01 AM
LXer: How to Setup ZSH and Oh-my-zsh on Linux LXer Syndicated Linux News 0 11-02-2017 12:42 AM
[SOLVED] Bash: Checking for lower case and upper case letters in a string fatalerror0x00 Programming 1 12-09-2012 02:17 AM
Help with sed and awk to change L-case letters to U-case for specific lines in a file rootaccess Linux - General 12 05-21-2012 02:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration