Solved

Split array in KSH

Posted on 2013-01-15
8
765 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

920 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

17 Experts available now in Live!

Get 1:1 Help Now