Link to home
Start Free TrialLog in
Avatar of skahlert2010
skahlert2010

asked on

Split array in KSH

I need help in splitting an array into chunks.

I build an array from a file and echo the array content:

set -A array `grep $SRV_NAME /myfile| grep DBA| awk -F# '{print $4}'`
echo ${array[*]}

Open in new window


The output is:

DBA1
DBA2
DBA3
DBA4

Open in new window


When I count the array members, I however get only one member whereas I need to have 4.

print ${#array[*]} 

Open in new window


Can you please show me how to solve this problem or what I am doing wrong. How can I split the array into chunks?
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

For some very strange reason there are linefeeds between the array elements.

Under normal circumstances this should not happen when using command substitution for array creation.

Could it be that "/myfile" contains some unusual characters, maybe because it came from a non-Unix machine?

Please examine this file, e.g. with "cat -v /myfile".
What do you see?
Avatar of skahlert2010
skahlert2010

ASKER

Hi woolmilkpork!

Don't be irritrated by the linefeed.

I didn't copy and paste the content.

This is the original content:

K01TDB
GRIDCTL
READDB
KT01STDB
KT02STDB

Any ideas?
It's still the linefeeds!

The array elements should never appear each on a line of its own.
Or is this also a copy-and-paste thing?

You should rather see this:

K01TDB GRIDCTL READDB KT01STDB KT02STDB
These elements are grepped from different lines within the source file. They are not part on a single line!

The original output looks like:

1#K01TDB1#DBA#K01TDB
3#GRIDCTL1#DBA#GRIDCTL
4#READDB#DBA#READDB
5#KT01STDB1#DBA#KT01STDB
9#KT02STDB1#DBA#KT02STDB

With awk I cut of everything in front of the values.

From then on I don't know how to store these elements in an array individually.
I need to make a loop later on based on the values in that array.
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay, I must have made a mistake since I am very new to shell skripting.

The following works for me:

set -A DBSIDS |\
for DBA_DBNAME in `grep $SRV_NAME /oracle/admin/config/dbaconf | grep DBA_DBNAME | awk -F# '{print $4}'`
do 
echo $DBA_DBNAME
done

print The array contains ${#DBSIDS[*]} elements!

Open in new window


Output:

K01TDB
GRIDCTL
READDB
KT01STDB
KT02STDB

The array contains 5 elements!
Not the solution itself but made me try something that solved my problem!
I never saw such a construct ("set -A xxx | something ...") before, and I couldn't reproduce any sensible effect.
What you see in DBSIDS[*] are old data from some previous attempts.

Anyway, thx for the points.