How to change the 1st letter of a filename to uppercase by sh script???

How to change the 1st letter of a filename to uppercase by sh script???

Post by Hugo » Tue, 08 Apr 2003 23:33:21



Hi all experts, I am new for unix shell scripting. Can anyone tell me how to
change a filename from lowercase to uppercase by sh script? In addition, I
only need to chnage the first letter of the filename i.e. "filename" --->
"Filename".

All I know is the command-----tr "[a-z]" "[A-Z]"

But this will change all letters to uppercase?

Any experts pls help me.

 
 
 

How to change the 1st letter of a filename to uppercase by sh script???

Post by Chris F.A. Johnso » Wed, 09 Apr 2003 00:55:10



> Hi all experts, I am new for unix shell scripting. Can anyone tell me how to
> change a filename from lowercase to uppercase by sh script? In addition, I
> only need to chnage the first letter of the filename i.e. "filename" --->
> "Filename".

> All I know is the command-----tr "[a-z]" "[A-Z]"

> But this will change all letters to uppercase?

    Separate the first letter, change that, then replace the first
    letter of the filename with the upper-case letter.

    These assume that the directory portion of the filename has
    already been stripped off:

allbutfirst=`echo $filename | cut -c2-`
firstletter=`echo $filename | cut -c1`
capname=`echo $firstletter | tr '[a-z]' '[A-Z]'`$allbutfirst

    With a POSIX shell, the first two lines can be replaced with:

allbutfirst=${filename#?}
firstletter=${filename%$allbutfirst}

    With bash, no external commands are needed:

lower=thequickbrownfxjmpsvlazydg
upper=THEQUICKBROWNFXJMPSVLAZYDG
allbutfirst=${filename:1}
firstletter=${filename:0:1}
case $firstletter in
   [a-z]) idx=${lower%$firstletter*}
          firstletter=${upper:${#idx}:1}
          ;;
esac
capname=$firstletter$allbutfirst

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

How to change the 1st letter of a filename to uppercase by sh script???

Post by David Thompso » Wed, 09 Apr 2003 01:28:34



Quote:>     With a POSIX shell, the first two lines can be replaced with:

> allbutfirst=${filename#?}
> firstletter=${filename%$allbutfirst}

Is 'typeset -l' part of POSIX?

  allbutfirst=${filename#?}
  typeset -l firstletter=${filename%$allbutfirst}

The above works for me in ksh88/ksh93, but I see that
bash doesn't support the -l option,

  http://www.gnu.org/manual/bash/html_mono/bashref.html

searching for 'typeset', I found this,

  typeset
    typeset [-afFrxi] [-p] [name[=value]]

  The typeset command is supplied for compatibility with
  the Korn shell; however, it has been deprecated in favor
  of the declare builtin command.

I've been trying to confirm/deny 'typeset -l' in POSIX, but
I'm not having much luck with http://www.opengroup.com, am
I looking in the right place?

--
David Thompson

 
 
 

How to change the 1st letter of a filename to uppercase by sh script???

Post by Ben » Wed, 09 Apr 2003 01:35:03



> I've been trying to confirm/deny 'typeset -l' in POSIX, but
> I'm not having much luck with http://www.opengroup.com, am
> I looking in the right place?

Can't give you links off hand, but I beleive it is Korn shell specific.

regards,
Ben

 
 
 

How to change the 1st letter of a filename to uppercase by sh script???

Post by Stephane CHAZELA » Wed, 09 Apr 2003 01:43:37



> Hi all experts, I am new for unix shell scripting. Can anyone tell me how to
> change a filename from lowercase to uppercase by sh script? In addition, I
> only need to chnage the first letter of the filename i.e. "filename" --->
> "Filename".

With zsh, it would be:

mv -i -- $file ${(C)file}

With other shells:

exec 3<&0
sed "s/'/&\\\\&&/g
     1h;1!H;\$!d;g;s/\(.\).*/\1/
     y/azertyuiopqsdfghjklmwxcvbn/AZERTYUIOPQSDFGHJKLMWXCVBN/
     G;s/^\(.\)\n.\(.*\)/'\1\2' <&3/
     x;s/.*/mv -i -- '&' \\\\/;G" << EOF | sh
$file
EOF
exec 3<&-

That doesn't turn '' into ''.

--
Stphane

 
 
 

How to change the 1st letter of a filename to uppercase by sh script???

Post by Ben » Wed, 09 Apr 2003 01:53:23



> Hi all experts, I am new for unix shell scripting. Can anyone tell me how to
> change a filename from lowercase to uppercase by sh script? In addition, I
> only need to chnage the first letter of the filename i.e. "filename" --->
> "Filename".

> All I know is the command-----tr "[a-z]" "[A-Z]"

> But this will change all letters to uppercase?

> Any experts pls help me.

Try:
    ls | perl -ne 'chomp;rename($_, ucfirst)'

regards.
Ben

 
 
 

How to change the 1st letter of a filename to uppercase by sh script???

Post by Stephane CHAZELA » Wed, 09 Apr 2003 02:02:07



> Hi all experts, I am new for unix shell scripting. Can anyone tell me how to
> change a filename from lowercase to uppercase by sh script? In addition, I
> only need to chnage the first letter of the filename i.e. "filename" --->
> "Filename".

[...]

with POSIX shells :

case $file in
[a-z]*)
rest=${file#?}
first=${file%"$rest"}
tr=aAzZeErRtTyYuUiIoOpPqQsSdDfFgGhHjJkKlLmMwWxXcCvVbBnN
ufirst=${tr#*"$first"}
ufirst=${ufirst%%"${ufirst#?}"}
mv -i -- "$file" "$ufirst$rest"
;;
esac

--
Stphane

 
 
 

How to change the 1st letter of a filename to uppercase by sh script???

Post by Stephane CHAZELA » Wed, 09 Apr 2003 02:02:07


[...]
Quote:> allbutfirst=`echo $filename | cut -c2-`
> firstletter=`echo $filename | cut -c1`
> capname=`echo $firstletter | tr '[a-z]' '[A-Z]'`$allbutfirst

[...]

Note that this doesn't work with file names with newlines or
tabs or consecutive or trailing blanks or backslashes in them.

--
Stphane

 
 
 

How to change the 1st letter of a filename to uppercase by sh script???

Post by Stephane CHAZELA » Wed, 09 Apr 2003 02:02:08


[...]
Quote:> Is 'typeset -l' part of POSIX?

[...]

No there's no typing, so no typeset command in POSIX shells.

--
Stphane

 
 
 

How to change the 1st letter of a filename to uppercase by sh script???

Post by Chris F.A. Johnso » Wed, 09 Apr 2003 02:22:58




> [...]
>> allbutfirst=`echo $filename | cut -c2-`
>> firstletter=`echo $filename | cut -c1`
>> capname=`echo $firstletter | tr '[a-z]' '[A-Z]'`$allbutfirst
> [...]

> Note that this doesn't work with file names with newlines or
> tabs or consecutive or trailing blanks or backslashes in them.

    Quite true; it only works with sensibly named files.

    To handle spaces and tabs is easy; just enclose $filename in
    quotation marks.

    File names with newlines in them are a perversity and don't
    deserve special attention other than to fix them. They break many
    otherwise robust scripts, and make any scripting involving file
    names a needless nightmare.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

How to change the 1st letter of a filename to uppercase by sh script???

Post by David Thompso » Wed, 09 Apr 2003 04:21:04




> >     With a POSIX shell, the first two lines can be replaced with:

> > allbutfirst=${filename#?}
> > firstletter=${filename%$allbutfirst}

> Is 'typeset -l' part of POSIX?

>   allbutfirst=${filename#?}
>   typeset -l firstletter=${filename%$allbutfirst}

Using ksh93,

    typeset -l firstletter="${filename:0:1}"
    allbutfirst="${filename:1}"

--
David Thompson

 
 
 

How to change the 1st letter of a filename to uppercase by sh script???

Post by William Par » Wed, 09 Apr 2003 07:00:10



> Hi all experts, I am new for unix shell scripting. Can anyone tell me how to
> change a filename from lowercase to uppercase by sh script? In addition, I
> only need to chnage the first letter of the filename i.e. "filename" --->
> "Filename".

> All I know is the command-----tr "[a-z]" "[A-Z]"

> But this will change all letters to uppercase?

> Any experts pls help me.

You are on right track.  Just apply it to the first letter, ie.
    mv $i `echo ${i:0:1} | tr ...`${i:1}

--

Linux solution for data management and processing.

 
 
 

How to change the 1st letter of a filename to uppercase by sh script???

Post by Ben » Wed, 09 Apr 2003 21:21:20




>> Hi all experts, I am new for unix shell scripting. Can anyone tell me
>> how to
>> change a filename from lowercase to uppercase by sh script? In
>> addition, I
>> only need to chnage the first letter of the filename i.e. "filename" --->
>> "Filename".

>> All I know is the command-----tr "[a-z]" "[A-Z]"

>> But this will change all letters to uppercase?

>> Any experts pls help me.

> Try:
>    ls | perl -ne 'chomp;rename($_, ucfirst)'

Or what was just pointed out to me in the perl group:
    ls | perl -lne 'rename($_, ucfirst)'

regards,
Ben

 
 
 

How to change the 1st letter of a filename to uppercase by sh script???

Post by Randal L. Schwar » Thu, 10 Apr 2003 00:20:51


Ben> Or what was just pointed out to me in the perl group:
Ben>     ls | perl -lne 'rename($_, ucfirst)'

Or even far better, because newline-containing names won't
mess it up:

perl -e 'rename $_, ucfirst for <*>'

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

 
 
 

How to change the 1st letter of a filename to uppercase by sh script???

Post by Ben » Thu, 10 Apr 2003 00:43:56



> Ben> Or what was just pointed out to me in the perl group:
> Ben>     ls | perl -lne 'rename($_, ucfirst)'

> Or even far better, because newline-containing names won't
> mess it up:

> perl -e 'rename $_, ucfirst for <*>'

Wow! I didn't realise that <> could take wildcards.

Thanks,
Ben

 
 
 

1. How to convert 1st letter of string to uppercase ??

        Is there a way to convert the 1st letter of a string to uppercase
        if it is one of [a-z,A-Z], the rest is in lowercase
        Anything in sh, or tcsh, or csh, or awk will do.

        eg.     s = aLineOfText
                1sttoupper(s) gives 'Alineoftext'

        Thanks for any help.
        ---
        minh
--
|>   B\iochemistry                        Minh Tra/N                       <|

|>     S\ervice                     (03) 345-590/8                         <|
|>      D\epartment         Melbourne, Australi/A                          <|

2. Unix(&Novell) backup systems

3. Execute a sh script under perl and sh: sh script; perl script?

4. shared libs

5. change filenames from uppercase to lowercase

6. Linux RAID-5 Problem w/ raidtools-0.90??

7. Changing filename from lower to Uppercase - all directories

8. Managing Remote Desktop Sessions

9. changing filenames from uppercase to lowercase

10. simple shell script to append some letters to filenames...

11. how to change the case of a letter in a filename with mv?

12. Changing case of first letter in filename

13. Changing passwords from a sh scripts (or csh script)