Array Retrieval Formatting

I have a shell script (Solaris v8 ksh) loading an array from a flat file. An exaple of that flat file is below. It contains white-spaces, and carrage returns. When I try to load the first line as a variable, it (the variable) shows up as the first field not the first line. How do you arrange an array by carrage return? I know it can be done, but can't find anywhere that talk about it. Thanks in advance for replies, this forum rocks!

description.lst snippet ->
API Programmers Reference
Backup Procedures for Vista Plus on UNIX
The Vista Backup and Recovery Plan
Vista Plus Check Generations Usage Guide
-------------------------------------------------------------------------
shell scrip snippet->
set -A description $(cat $SHDIR/description.lst $1)
CNT=0
REPDESC=${description[$CNT]}
-------------------------------------------------------------------------
If I echo $REPDESC I get:
API
I would like it to echo:
API Programmers Reference

I have tried putting the contents of description.lst into � � to no avail.

OK just noticed something. If I change the:
set -A reportid $(cat $SHDIR/description.lst $1)
to
set -A reportid $(cat $SHDIR/description.lst $0)
maybe it may work. I will check real quick.

nope didn't change the results. Oh well, anyone??

The problem here is that the array is populated based on space-separated items. The only way I know to get around it is to change the spaces to something else and change them back later when you are ready to print. Not pretty.

djp

Try this:

IFS='
'
set -A description $(cat $SHDIR/description.lst $1)

If only I'd waited a little longer before displaying my ignorance! :eek:

Seriously, thanks for posting the answer, tmarikle. I've been needing that for awhile and it never occurred to me...

djp

No worries.

One thing should be mentioned. IFS typically contains a space, a tab, and a newline character. You may want to save IFS's original value or typeset it in a function when changing it as it may change how your other scripting constructs tokenize lists. This way, IFS can be restored back to its original field separators.

Example:

OLD_IFS=${IFS}
IFS='
'
set -A ...
IFS=${OLD_IFS}

Thomas

Wow it works like a champ, how cool is that? Thanks to the both of you for you help.

So I Googled around a little, and everyone shows how to do use it but not really any discription on what exactly it is. Let me see if I understand correctly.
IFS = Input Field Seperator; does that work for any kind of input like getline or a filename.sh < filename.lst etc.? Thanks again!

yes, it does work on every form of input where "fields" are regarded. It will, for instance, affect the way the statement "read word1 word2 remainder" works, because a "word" is basically something surrounded by two IFS chars, whatever that may be (or BOF/EOF, of course).

Another solution to your problem is to read in your file in a loop:

(( cnt = 0 ))
cat <file> | while read line ; do
     typeset myarr[$cnt]="$line"
     (( cnt += 1 ))
done

But notice, that it is necessary to protect (by quoting) your resulting variables. Once you set $IFS back to its original value you can't rely on the array elements not being split up into words if you do not surround them with double quotes.

bakunin

Right on, thanks bakunin. I am taking an alternate approach with this script. I havn't worked with functions much and arrays at all, so I am making a script entirely based on this to attempt to broaden by abilities. The script looks really tidy, and probably runs faster setup this way; rather then reading an endless amount of awk statements, for loops etc. Thanks to all for the help.