Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Split array in KSH

Posted on 2013-01-15
8
Medium Priority
?
794 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 1500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

636 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