(help!) change first char to upper case

(help!) change first char to upper case

Post by Hong Lia » Mon, 29 Mar 1993 14:08:58



A simple question, please help!

In a shell program, how can I change the
first character of a string to upper case
if it is in lower case?

     Ex:    unix -> Unix
            Foo  -> Foo    

Thanks a lot!

-Hongliang

 
 
 

(help!) change first char to upper case

Post by Rouben Rostami » Mon, 29 Mar 1993 16:42:34



>In a shell program, how can I change the
>first character of a string to upper case
>if it is in lower case?

>     Ex:    unix -> Unix
>            Foo  -> Foo    

0.  Warning.  It is possible that I have fail to see an obvious
and easy solution.  Proceed at your own risk.

1. Using nawk or gawk:

newstr=`echo $oldstr | nawk '{ printf "%s%s", toupper(substr($0,1,1)), substr($0,2) }'`

Comment: Not all systems have nawk or gawk

2. With expr and tr:

newstr=`expr $oldstr : '\(.\).*' | tr a-z A-Z``expr $oldstr : '.\(.*\)'`

Comment: This is quite wasteful in the number of processes it spawns

3. With sed:

newstr=`echo $oldstr | sed -f scrptfle`

where the file "scrptfle" contains the following sed script:

h
s/.\(.*\)/\1/
x
s/\(.\).*/\1/
y/abcefghijklmnopqrstuvwxyz/ABCEFGHIJKLMNOPQRSTUVWXYZ/
G
s/\n//

Comment 1: This is ugly, but it's fast and efficient.
Comment 2: The y operator is an undocumented feature of sed, so it
may not be available on all versions of sed.

4. With perl

Solution left as an expercise :-)

 
 
 

(help!) change first char to upper case

Post by Timothy Kimba » Tue, 30 Mar 1993 23:37:45


: In a shell program, how can I change the
: first character of a string to upper case
: if it is in lower case?
:  
:      Ex:    unix -> Unix
:             Foo  -> Foo    

This will capitalize a specific word:

sed 's/\<unix/Unix/g' filname

The \< construct tells sed to match the beginning of a word.

Hope that helps some.

--
/* tdk  --  Science Computing and Research Support (SCARS)  --  STScI */
/* Disclaimer:  The above remarks are my My MY personal observations. */

 
 
 

(help!) change first char to upper case

Post by DW » Wed, 31 Mar 1993 02:46:18



| The \< construct tells sed to match the beginning of a word.

Not all versions of sed honor '\<' and '\>'.  If any given regexp magic isn't
supported by ed and isn't in your manual page for sed, sed very likely might
not support it, even if egrep or ex does.

There's an old saying that if sed were based on ex rather than ed it would
have been called sex.

David W. Tamkin   Box 59297   Northtown Station, Illinois  60659-0297

 
 
 

1. How to change case of first letter Upper case only.

hello,
   i have this file:

     x9999,77,GHI JKL,abc
     x9998,67,IJKL MNOP,bcd
     x9997,47,JKL M,cde
     x9996,23,KLMN OP QRST,def

   using vi, how do i do in order to get the 3rd field changed and the
new file looks like this:

     x9999,77,Ghi Jkl,abc
     x9998,67,Ijkl Mnop,bcd
     x9997,47,Jkl M,cde
     x9996,23,Klmn Op Qrst,def

thanks.
-kuman-

2. X Windows Display problem

3. How to convert case to first letter Upper case only.

4. NIS+ table.

5. change lower case word to upper case using sed?

6. Performance problems with connection sharing

7. Change Upper case filename to lower case filename

8. ownership of an incoming ftp file

9. Change upper case file names into lower case

10. script to change lower case to upper case

11. changing the case but only the first char

12. Help: how to convert lower case to upper case?

13. char *strcasestr(char *haystack, char *needle) a simple case independent strstr()