Operating System - HP-UX
1839697 Members
2394 Online
110153 Solutions
New Discussion

urgent : Korn shell programmig using set

 
Balasubramanian
Occasional Contributor

urgent : Korn shell programmig using set

Hi all,

I am using set array option to store the records into fields and then compare.

What exactly happening is when the

field contains null spaces. the array is truncating it.


For ex. when the field contains the value
"1 ", the array truncates the remaining spaces after 1. But i want to compare as it is with out truncation using
set -A option.

How to avoid truncation of fields with trailing null spaces ?

Because of this truncation i am getting wrong results.

One more thing what i need is,

when the field value is more than expected length, i have to truncate to specified length.

For ex.

set -A fields2 $(print "$line2" | tr "\|" " "

fieldlen=`expr length "${fields3[$y]}"`

if [ $fieldlen -gt 12 ]
then
fields2[$y]=`expr substr "${fields2[$y]}" 1 12`
else
:
fi


Can i assign the value to array of strings this way ?

How do i reassign the value to an array ?


I have given below the sample code.



####Sample Code ####

exec 3 < file1

exec 5 < file2

while read -u3 line1
do

no_of_fields=`echo $line1" | awk '{ FS = "|" } { print NF }`

set -A fields1 $(print "$line1" | tr "\|" " ")

while read -u5 line2
do

set -A fields2 $(print "$line2" | tr "\|" " "
count=$no_of_fields
cmpfile3=0
y=0
while [ $count -gt 0 ]
do

fieldlen=`expr length "${fields3[$y]}"`

if [ $fieldlen -gt 12 ]
then
fields1[$y]=`expr substr "${fields1[$y]}" 1 12`
fields3[$y]=`expr substr "${fields3[$y]}" 1 12`
else
:
fi


........

#########



Awaiting your reply.


Thanks
Bala
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: urgent : Korn shell programmig using set

Hi:

> To preserve spaces as a legitimate field, change your IFS variable in your script, like:

IFS=: #...changes the inter-field-separator from the standard space to the colon character.

> To change the value of an array element, do, for example:

ARY[3]=" XYZ "

...JRF...
RikTytgat
Honored Contributor

Re: urgent : Korn shell programmig using set

Bala,

First question: this is what I have:

$ set -A arrTest " 1 "
$ echo --${arrTest[0]}--
--1--
$ IFS=""
$ set -A arrTest " 1 "
$ echo --${arrTest[0]}--
-- 1 --
$ unset IFS

Is that what you desire??

Do not forget to unset IFS after you're ready with it. For more info on IFS, check out the sh-posix manpage.

Hope this helps,
Rik
RikTytgat
Honored Contributor

Re: urgent : Korn shell programmig using set

Bala,

For your field length problem, you might try to substr before storing the variables into the array.

set -A fields $(echo $line | awk '{ for (i=1;i<=NF;i++) print substr($i,1,12) }')

This should limit every word on $line to 12 characters.

Hope this is what you want,
Rik.
Balasubramanian
Occasional Contributor

Re: urgent : Korn shell programmig using set

Hi friends,

Thanks for your help in resolving the problem.


-Bala
Dan Hetzel
Honored Contributor

Re: urgent : Korn shell programmig using set

Hi Bala,

It's nice to say 'Thanks' but it would be a lot nicer if you assigned points to the people who spent some time (or a lot of) to help you.

Thanks for them,

No point for me please !

Dan

Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Carlos Fernandez Riera
Honored Contributor

Re: urgent : Korn shell programmig using set

Have you read man sh-posix?

A world of posibilities:

From man sh-posix


& typeset [{-|+}LRZfilrtux[n]] [name[=value]]...
name=value [name=value]...

Assign types and a value to a local named parameter name. See
also the export special command. Parameter assignments remain in
function, create a new instance of the parameter name. The
parameter value and type are restored when the function
completes.

The following list of attributes can be specified. Use + instead
of - to turn the options off.

-L Left justify and remove leading blanks from value. If n is
nonzero, it defines the width of the field; otherwise, it is
determined by the width of the value of first assignment.
When name is assigned, the value is filled on the right with
blanks or truncated, if necessary, to fit into the field.
Leading zeros are removed if the -Z option is also set. The
-R option is turned off. Flagged as leftjust n.

-R Right justify and fill with leading blanks. If n is
nonzero, it defines the width of the field; otherwise, it is
determined by the width of the value of first assignment.
The field is left-filled with blanks or truncated from the
end if the parameter is reassigned. The -L option is turned
off. Flagged as rightjust n.

-Z Right justify and fill with leading zeros if the first
nonblank character is a digit and the -L option has not been



:-))
unsupported