Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to change case

Status
Not open for further replies.

ianicr

IS-IT--Management
Nov 4, 2003
230
GB
Hi
I have a file all in uppercase that I wish to change to sentence case eg.
SMITH
becomes
Smith
Anyone got any ideas?
Thanks
 
Hmmmm
Use tr to get to all lower case :
tr &quot;[:upper:]&quot; &quot;[:lower:]&quot; < capsfilename > lowfilename

Then, depending on your rules, use n/g/awk to do an upper
on the first character in each field - Can you post an example ?

Dickie Bird (:)-)))
 
Try something like this:
Code:
awk '{
for(i=1;i<NF;++i)
 printf &quot;%s%s &quot;,substr($i,1,1),tolower(substr($i,2))
printf &quot;%s%s\n&quot;,substr($NF,1,1),tolower(substr($NF,2))
}' /path/to/input >output

Hope This Help
PH.
 
Using awk...

echo SMITH | awk '{print substr($1,1,1) tolower(substr($1,2))}'
 
If you want to change all content to become sentence case. I wrote a script as follows:

#turn all to lower
y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/

#change first letter of first line to upper
1 {
/^[a-z][^.]*.*$/ {
h
s/^\([a-z]\).*$/\1/
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
G
s/^\(.*\)\n\([a-z]\)\(.*\)$/\1\3/
}
}

#turn any word after full stop to upper
:loop
/\. [a-z]/ {
h
s/^[^.]*\(\. [a-z]\).*$/\1/
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
G
s/^\(\. [A-Z]\)\n\(.*\)\(\. [a-z]\)\(.*\)$/\2\1\4/
b loop
}

# if full stop appears in last position, change letter to upper in next line
/[a-z]\{2,\}\.$/ {
n
y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
/[a-z][^.]*.*$/ {
h
s/\([a-z]\)\(.*\)/\1/
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
G
s/\(.*\)\n\([a-z]\)\(.*\)/\1\3/
b loop
}
}

#Should do this replacement in last part of the script
/^$/ {
n
y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
/^[a-z][^.]*.*$/ {
h
s/^\([a-z]\).*$/\1/
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
G
s/^\(.*\)\n\([a-z]\)\(.*\)$/\1\3/
b loop
}
}


Hope it may be useful for you. If it can't handle any cases pls. let me know.

tikual
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top