1832602 Members
2316 Online
110043 Solutions
New Discussion

Re: bust up a variable

 
SOLVED
Go to solution
Donny Jekels
Respected Contributor

bust up a variable

this might be easy to do for most, but I am having a mental block with it.

I have a variable that contains all-capps of a persons last name.

1. how do i break up the variable into variables that only contain the characters?

2. how do I change from caps to lowercase?

3. how do i capitalize the 1st character?

4. also need to comply to creating userid's from these names, where the uid must be the first 7 characters of the last name and the 1st let of the first name.

5. also some last names are less than 7 characters, in that case more charaters must be used from the first name to make up 8 characters.

in ksh please.

I have 300 user accounts to create and they provided me with lastnames in caps in first names in lowercase.

Donny
"Vision, is the art of seeing the invisible"
9 REPLIES 9
Michael Steele_2
Honored Contributor

Re: bust up a variable

Case sensitivity:

typeset -u VAR=abc
echo $VAR
ABC

typeset -l VAR=ABC
echo $VAR
abc

For putlling char's out of a string:

FOURTH=$(echo $VAR | cut -c 4)

TWO=$(echo $VAR | cut -c 2)

etc.

For question #5, about 7 or 8 chars.

Build your EIGHT VAR's and manipulate them anyway you want.

ONE
TWO
...
SEVEN = NULL?
EIGHT = NULL?

etc.

Get a copy of "The New Kornshell Command Reference " by Bolsky and Korn.
Support Fatherhood - Stop Family Law
Donny Jekels
Respected Contributor

Re: bust up a variable

if I understand you correctly then, if I already have a variable with information in it and I know its CAPS, then:

LASTNAME=ANDERSON
typeset -l $LASTNAME
echo $LASTNAME
anderson

or did I loose it all together.
"Vision, is the art of seeing the invisible"
James A. Donovan
Honored Contributor
Solution

Re: bust up a variable

To set all the last names to lower case you could also use the tr utility.

> LASTNAME=DONOVAN
> echo $LASTNAME| tr "[:upper:]" "[:lower:]"
> donovan

and if your new user's names are ina single file then you can use tr as follows:

> cat filename|tr "[:upper:]" "[:lower:]" >lowercase

and all uppercase in "filename" will be translated to lowercase in "lowercase"

...it would be
typset -l LASTNAME=ANDERSON
echo $LASTNAME
anderson

Rule #5 seems contrived to me. What happens when you eventually have a user whose first and last names combined do not total 8 characters?
Remember, wherever you go, there you are...
curt larson_1
Honored Contributor

Re: bust up a variable

here is a quick awk script,

#!/usr/bin/ksh

fname=
lname=

print "$fname $lname" | awk '
function modname(string) {
len=length(string);
if (len>7) len=6;
fchar=toupper(substr(string,1,1));
rest=tolower(substr(string,2,len));
return fchar rest
}
{
name=modname($1) modname($2);
len=length(name);
if (len>7) name=substr(name,1,8);
print name;
}
Donny Jekels
Respected Contributor

Re: bust up a variable

Jim,

thanx for your tutorial

as for #5,
I think, one would want to use all the users last name, and oh, now I remember.

you start using charaters in the users first name.

and I guess, if the users last and first name combined is less than 8, they want it to be populated with something unique

interresting ....
"Vision, is the art of seeing the invisible"
curt larson_1
Honored Contributor

Re: bust up a variable

just using the shell:

#!/usr/bin/ksh

fname=
lname=

function modname {

typeset -u fchar
typeset -l rest

fchar=$(print $1 | cut -c 1)
rest=$(print $1 | cut -c 2-)

print "${fchar}$rest"
return
}

fname=$(modname $fname)
len=${#fname}
if [ $len > 7 ] ;then
fname=$(print $fname | cut -c 1-7)
fi
lname=$(modname $lname)
name="${fname}$lname"
len=${#name}
if [ $len > 8 ] ;then
name=$(print $name | cut -c 1-8)
print $name



Donny Jekels
Respected Contributor

Re: bust up a variable

jim & curt,

thanx, a million. I am ok from here on. will send you the final script for critique

10 point for both.
"Vision, is the art of seeing the invisible"
curt larson_1
Honored Contributor

Re: bust up a variable

to add some characters to the end:

elif [ $len < 8 ] ;then

set -A Chars a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 . /

while [ $len < 8 ]
do
num=$(($RANDOM / 512 ))
char=${Chars[$num]}
name="${name}$char"
len=${#name}
done
Donny Jekels
Respected Contributor

Re: bust up a variable

great, this is turning out to be a killer script.
"Vision, is the art of seeing the invisible"