Solved

Split array in KSH

Posted on 2013-01-15
8
761 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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
The viewer will learn how to dynamically set the form action using jQuery.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now