Operating System - Linux
1758025 Members
1776 Online
108866 Solutions
New Discussion юеВ

converting uppercase to lowercase characters

 
SOLVED
Go to solution
Klaas D. Eenkhoorn
Regular Advisor

converting uppercase to lowercase characters

dear all
How do i convert a word written in all uppercase caracters to a word with only the first character uppercase and the rest lowercase ?

example:

"WOORD IN EEN ZIN" -> "Woord In Een Zin"

Thanks in advance

kl@@s


10 REPLIES 10
H.Merijn Brand (procura
Honored Contributor
Solution

Re: converting uppercase to lowercase characters

lt09:/home/merijn 107 > echo WOORD IN EEN ZIN | perl -lne'print join" ",map{ucfirst lc}split'
Woord In Een Zin
lt09:/home/merijn 108 > echo WOORD IN EEN ZIN | perl -pe's/(\w+)/\L\u$1/g'
Woord In Een Zin
lt09:/home/merijn 109 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Klaas D. Eenkhoorn
Regular Advisor

Re: converting uppercase to lowercase characters

Line 1 gives no output and line 2 does not change the caps.

am i doing something wrong ?

klaas

H.Merijn Brand (procura
Honored Contributor

Re: converting uppercase to lowercase characters

Do you have an `old' perl?

if you get

# perl -v

This is perl, version 4.0

$RCSfile: perl.c,v $$Revision: 4.0.1.8 $$Date: 1993/02/05 19:39:30 $
Patch level: 36

Copyright (c) 1989, 1990, 1991, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 4.0 source kit.

you have an ancient perl installed on /usr/contrib/bin, and that version does not support the fact that the script (the -e part) is tight to the -e option without space separation :(

a5:/u/usr/merijn 101 > echo WOORD IN EEN ZIN | perl -pe's/(\w+)/\L\u$1/g'
Woord In Een Zin
a5:/u/usr/merijn 102 > echo WOORD IN EEN ZIN | perl4 -pe's/(\w+)/\L\u$1/g'
WOORD IN EEN ZIN
a5:/u/usr/merijn 103 > echo WOORD IN EEN ZIN | perl4 -pe 's/(\w+)/\L\u$1/g'
Woord In Een Zin
a5:/u/usr/merijn 104 >

If you only have that perl4, just add a space, as above example shows.
My advice would be to chack if you have other versions of perl installed, and move /usr/contrib/bin to the *end* of your $PATH.
Almost every perl script snippet posted on the forum expects at least perl-5.005_3, and I only use perl-5.8.5

more modern perls are usually installed as either /usr/local/bin/perl or in /opt/perl/bin/perl

[ Leuk om weer een nieuwe nederlandstalige te mogen verwelkomen :) ]

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Pete Randall
Outstanding Contributor

Re: converting uppercase to lowercase characters

echo "WOORD IN EEN ZIN" | tr "[:upper:]" "[:lower:]"


Pete

Pete
H.Merijn Brand (procura
Honored Contributor

Re: converting uppercase to lowercase characters

Pete, that would make the complete line lower

Not what he wants.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Pete Randall
Outstanding Contributor

Re: converting uppercase to lowercase characters

Right you are, Merijn. I didn't read carefully enough.


Pete

Pete
Geoff Wild
Honored Contributor

Re: converting uppercase to lowercase characters

If you do run Pete's, this will capitalize the first letter of every word:

#! /bin/sed -f

# capit.sed -- capitalize words
#

# split words into \n word
s/[a-zA-Z][a-zA-Z]\+/\
&/g

# add conversion table: \n\n table
# table format:
s/$/\
\
AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz/

# subs every lower case first char
ta
:a
s/\n\(.\)\(.*\n\n.*\)\([A-Z]\)\1/\3\2\3\1/
ta

# cleanup...
s/\n\n.*//
s/\n//g



Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
john korterman
Honored Contributor

Re: converting uppercase to lowercase characters

Hi,

You can (mis)use ksh special features, e.g.:

#!/usr/bin/sh
LIST="WOORD IN EEN ZIN"
function convert
{
FUP="$1"
END_F=${FUP#?}
REST_LOW=$(echo "$END_F" | tr "[:upper:]" "[:lower:]")
typeset -u BEG_F=${FUP%$END_F}
echo "$BEG_F$REST_LOW"
}
for i in $LIST
do
convert "$i"
done

regards,
John K.
it would be nice if you always got a second chance
H.Merijn Brand (procura
Honored Contributor

Re: converting uppercase to lowercase characters

Beautifull all those scripts, but I still cannot imagine people actually write/use scripts that long if a simple

perl -pe 's/(\w+)/\L\u$1/g'

is all you need, and I even showed it works in perl4, which is available on every HP-UX box older than 11i (11.11 and up have perl5 installed by default)

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn