Solved

Split array in KSH

Posted on 2013-01-15
8
769 Views
Last Modified: 2013-01-15
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?
0
Comment
Question by:skahlert2010
  • 4
  • 4
8 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38777807
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?
0
 

Author Comment

by:skahlert2010
ID: 38777840
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?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38777860
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
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:skahlert2010
ID: 38777964
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.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 38778037
Yes,

I'm well aware of what you're doing and what you're trying to achieve.

Remains my question:

With

echo ${array[*]}

do you really see the extracted values one per line?

That's the important part here! I tested using your data, and the result was as I expected:

K01TDB GRIDCTL READDB KT01STDB KT02STDB

and echo ${#array[*]}

gives "5".

(I substituted "SRV_NAME" with "DBA" because I didn't find another common element)

So your command works just fine for me. I still suspect there are weird characters in the inputfile.

Again: What do you see with "cat -v /myfile"?
0
 

Author Comment

by:skahlert2010
ID: 38778137
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!
0
 

Author Closing Comment

by:skahlert2010
ID: 38778146
Not the solution itself but made me try something that solved my problem!
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38778169
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.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question