1753969 Members
7515 Online
108811 Solutions
New Discussion юеВ

Re: Name Changing

 
Edward Smith_1
New Member

Name Changing

I have a dilemia. I have a lot of files with mixed cases (upper and lower) (Example: fileEXAMPLE.SH, TESTfile.sh). I need to write a script to insert underscores between the case changes (Example: File named fileEXAMPLE.SH would change to file_EXAMPLE.SH). Can anyone help??

EDIT:
Script needs to be in the UNIX Korn Shell.
9 REPLIES 9
Dietmar Konermann
Honored Contributor

Re: Name Changing

The korn shell does not have real regular expressions... so I think the task would be difficult.

I would use the sed to do the actual replacement.

sed -e 's/\(^[a-z][a-z]*\)\([A-Z][A-Z]*\)/\1_\2/g' -e 's/\(^[A-Z][A-Z]*\)\([a-z][a-z]*\)/\1_\2/g'

Best regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Dietmar Konermann
Honored Contributor

Re: Name Changing

Oops, just saw that I left the '^' in the expressions. You should leave it the case changes more than once in a file name and you want multiple '_'.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Jean-Louis Phelix
Honored Contributor

Re: Name Changing

Hi,

Perhaps a shorter variant of Dietmar solution :

sed -e 's/[a-z][a-z]*/&_/g' -e 's/[A-Z][A-Z]*/&_/g' -e 's/_$//'

Regards
It works for me (┬й Bill McNAMARA ...)
Jean-Louis Phelix
Honored Contributor

Re: Name Changing

Sorry ... Dietmar solution seems better, because I have a problem with dots ! Although this one is not genral but works ...

sed -e 's/[a-z][a-z]*/&_/g' -e 's/[A-Z][A-Z]*/&_/g' -e 's/_$//' -e 's/_\././'

Regards.
It works for me (┬й Bill McNAMARA ...)
Dietmar Konermann
Honored Contributor

Re: Name Changing

Hi, Jean-Louis!

[NIT-PICK MODE ON]

What happens with e.g. "aaa111"?

[NIT-PICK MODE OFF]

:-)
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Jean-Louis Phelix
Honored Contributor

Re: Name Changing

Well Dietmar ... I could perhaps simply admit that your solution works and mine doesn't ? Or I could say that I only wanted to demonstrate the use of '&' :^) ...

By the way, I wanted to tell you that I apreciate a lot your contributions to this forum. Fast and efficient !

Regards.
It works for me (┬й Bill McNAMARA ...)
W.C. Epperson
Trusted Contributor

Re: Name Changing

Dietmar:

[NIT-NIT-PICK MODE ON]

Where's the case change in "aaa111"?

[NIT-NIT-PICK MODE OFF]

;)
"I have great faith in fools; self-confidence, my friends call it." --Poe
Jack Werner
Frequent Advisor

Re: Name Changing

Excuse me! Relational expressions are certainly part of and supported by Korn-shell. Reference the O'Reilly Korn-Shell Programming book.
i'm retired
Jerome Salyers
Advisor

Re: Name Changing

just for another perspective, i whipped up a little simple perl script which does this same thing, which a few checks...

if you like...

the file is already in UNIX... don't forget to change the directory of perl on the first line...

ciao

jerome

EDIT
the script can be run from ksh ;)

see attached