1830233 Members
2303 Online
109999 Solutions
New Discussion

script query

 
SOLVED
Go to solution
Ravinder Singh Gill
Regular Advisor

script query

Hi guys can anybody spot the problem with the following query?

while read SURNAME FIRSTNAME
do
typeset -l SNAME=$SURNAME | cut -c1-5

while [ ${#SNAME} -lt 5 ]
do
SNAME=$SNAME"a"
done

typeset -l FNAME=$FIRSTNAME | cut -c1

UNAME=`echo $SNAME$FNAME`
do
echo $UNAME
done
done < file1.txt


Basically from a list of names I am trying to create a list of Unames (first 5 characters of surname and first initial - "a" is used as padding if surname is less than 5 characters). I am using file1.txt for this. A sh -xvf shows the following:

while read SURNAME FIRSTNAME
do
typeset -l SNAME=$SURNAME | cut -c1-5

while [ ${#SNAME} -lt 5 ]
do
SNAME=$SNAME"a"
done

typeset -l FNAME=$FIRSTNAME | cut -c1

UNAME=`echo $SNAME$FNAME`
do
createuname.sh: Syntax error at line 14 : `do' is not expected.
14 REPLIES 14
RAC_1
Honored Contributor

Re: script query

set -vx and run script. It will tell you where it is erroring out.
There is no substitute to HARDWORK
Ravinder Singh Gill
Regular Advisor

Re: script query

how do i set that? can you give an example please
Muthukumar_5
Honored Contributor

Re: script query

You can use like this,


UNAME=`echo $SNAME$FNAME`
do
echo $UNAME
done
done < file1.txt

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: script query

You cannot use like this,


UNAME=`echo $SNAME$FNAME`
do
echo $UNAME
done
done < file1.txt

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: script query

sorry for typo(s) :)

Try as,

while read SURNAME FIRSTNAME
do
typeset -l SNAME=$SURNAME | cut -c1-5

while [ ${#SNAME} -lt 5 ]
do
SNAME=$SNAME"a"
done

typeset -l FNAME=$FIRSTNAME | cut -c1

UNAME=`echo $SNAME$FNAME`
echo $UNAME

done < file1.txt

It will work.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: script query

Really bad today :(.

Try this:

while read SURNAME FIRSTNAME
do

typeset -l SNAME=$(echo $SURNAME | cut -c1-5)

if [ ${#SNAME} -lt 5 ]
then
SNAME=$SNAME"a"
fi

typeset -l FNAME=$(echo $FIRSTNAME | cut -c1)

UNAME=`echo "$SNAME$FNAME"`
echo $UNAME

done < file1.txt

It will work.

hth.
Easy to suggest when don't know about the problem!
Ravinder Singh Gill
Regular Advisor

Re: script query

i did as above and still getting problems. when i did sh -xvf on it, it gave me output like the following:

+ read SURNAME FIRSTNAME
+ cut -c1-5
+ echoAncliff
createuname.sh[3]: echoAncliff: not found.
+ typeset -l SNAME=
+ [ 0 -lt 5 ]
+ SNAME=a
+ cut -c1
+ typeset -l FNAME=Gillian
+ + echo a
UNAME=a
+ echo a
a
+ read SURNAME FIRSTNAME
+ cut -c1-5
+ echoAnderson
createuname.sh[3]: echoAnderson: not found.
+ typeset -l SNAME=
+ [ 0 -lt 5 ]
+ SNAME=a
+ cut -c1
+ typeset -l FNAME=Jean
+ + echo a
UNAME=a
+ echo a
a
+ read SURNAME FIRSTNAME
+ cut -c1-5
+ echoAnderson
john korterman
Honored Contributor
Solution

Re: script query

Hi,

I think you are missing af space in between echo and a variable name in this line:

echo $SURNAME

regards,
John K.
it would be nice if you always got a second chance
Ravinder Singh Gill
Regular Advisor

Re: script query

thanks hth & John, that has solved half of my problem.

I am now getting the first five letters of their surname (all in lowercase as required) but their first initial is not being appended ie i am getting an output of adams rather then adamsc etc.

Any suggestions?
Hein van den Heuvel
Honored Contributor

Re: script query

One possible alternative solution in perl:


$perl -pe '($s,$f)=split; $_=substr(lc($s)."aaaa",0,5) . substr($f,0,1) . "\n"' x.txt

---gives---
aapjen
miesjt
vuuraz
jetjek

--- from x.txt ----
aapjes noot
MIEsje teun
vuur zus
jetje kees

fwiw,
Hein.
john korterman
Honored Contributor

Re: script query

Hi again,

Muthukumar's code works allright on my system, e.g. if the content of file1 is
Gill Ravinder
the script outputs
gillar

check variable spelling...

regards,
John K.



it would be nice if you always got a second chance
Ravinder Singh Gill
Regular Advisor

Re: script query

Thanks guys I realised my mistake. Just one more thing. One of the Surnames is only three characters long and this only appended "a" to the surname once, hence uname was only 5 characters long rather then 6. How would I amend the loop to fix this?
Ravinder Singh Gill
Regular Advisor

Re: script query

Guys thanks for your help. I just replaced the if loop with a while loop and it works fine.
Ravinder Singh Gill
Regular Advisor

Re: script query

Guys thanks for your help. I just replaced the if loop with a while loop and it works fine.