Manipulating a list into a two-dimensional array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Manipulating a list into a two-dimensional array
# 1  
Old 12-06-2012
Manipulating a list into a two-dimensional array

hi,
total newbie to shell scripting and wondering if some of you guru's can give me a hand on a problem I'm trying to solve.

The tmplsnr.a file contains

Code:
LSNR_51526
db1
db2
LSNR_51527
db3
db4
db5

Summary - depending on which db is set, the script will start the relevant listener (LSNR_). e.g if db5 is set then the script will start the LSNR_51527 listener.

I was thinking the best way maybe to put this information into an array and then search the array.

I want to read the above file into a two dimensional array and the array to looks like this so that I can search the corresponding LSNR for the db.
Code:
LSNR_51526,db1
LSNR_51526,db2
LSNR_51527,db3
LSNR_51527,db4
LSNR_51527,db5

How would I search the array for the listener (LSNR_51527) which relates to db5 ?

Would this method work or have I over complicated things or is there a much simpler method in korn scripting.

I would appreciate any feedback or help

Many Thanks in Advance

Last edited by mingy10; 12-06-2012 at 04:13 PM.. Reason: Please wrap data and sripts with CodeTags
# 2  
Old 12-06-2012
This will give you listener name based on the database name (assuming you are using Linux):
Code:
awk -vx="db5" '/^LSNR_/{l=$0}$0==x{print l}' tmplsnr.a

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 12-06-2012
Code:
awk '/^LSNR_/{lsn=$0}lsn!=$0{print lsn,$0}' OFS=, tmplsnr.a
LSNR_51526,db1
LSNR_51526,db2
LSNR_51527,db3
LSNR_51527,db4
LSNR_51527,db5

This User Gave Thanks to Yoda For This Post:
# 4  
Old 12-06-2012
Thank you Bartus11 ! that is so impressive - the power of awk in just one line - just to let you know i'm working on AIX and using Korn and your code works a treat. I had an issue at first because the tmplsnr.a wasn't formatted - it had spaces before the db's - i will need to sort that bit out in the future.

LSNR_51526
<spaces>db1
<spaces>db2
LSNR_51527
<spaces>db3
<spaces>db4
<spaces>db5
I have no idea how that works, but it's so cool, can you give me a quick explanation on how that piece of code works. Thanks
# 5  
Old 12-06-2012
To fix spaces problem:
Code:
awk -vx="db5" '/^LSNR_/{l=$0}$1==x{print l}' tmplsnr.a

The algorithm is pretty simple. It is saving name of the last encountered listener (line starting with LSNR) and prints it when it finds database name.
This User Gave Thanks to bartus11 For This Post:
# 6  
Old 12-06-2012
If you want to use bash/ksh/sh:
Code:
while read line                    
do                                 
if [ `echo $line|cut -c1-1` =  L ] 
then                               
        hdg=$line                  
else                               
        echo $hdg,$line            
fi                                 
done <data

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assign Two Dimensional Array In Bash At Once

Hi, I have a 10*10 two dimensional array. How do I assign value to all it's 100 elements at once? I don't want to open two for loops and assign one by one. Thanks, Shuri (1 Reply)
Discussion started by: shurimano
1 Replies

2. Shell Programming and Scripting

Multi Dimensional array

I have an array of names. Each one of the name, has a number represented to it. For example A has an ID 8, B has an ID 2. What I am after is a for loop that when the array is in position 1, a particular variable is set to the value of position 1 in array 2 declare -a arr=("A" "B" "C"... (6 Replies)
Discussion started by: nms
6 Replies

3. Shell Programming and Scripting

Storing two dimensional array for postprocessing

Hi Community, Would love to get some quick help on below requirement. I am trying to process mpstat output from multiple blades of my server I would like to assign this the output to an array and then use it for post processing. How can I use a two dimensional array and assign these value ... (23 Replies)
Discussion started by: sshark
23 Replies

4. Programming

Return two dimensional array in c++

I am writing matrix multiplication and trying to return a two dimensional array from a function but I keep getting errors. Can someone please help me? here is my code (it is just the skeleton of my program): void main () { ... int *matmultiply (int, int, int, int , int , int ) ... } ... (4 Replies)
Discussion started by: saboture88
4 Replies

5. Programming

C++ Segmentation error with bi-dimensional array

Hello, I'm experiencing a weird seg fault at run time when initializing a bi-dimensional array and initializing a class. Please see below code and comments describing the error and the instances when it occurs and when it does not occur. Compiled with g++ segf.cpp -o segf output On... (5 Replies)
Discussion started by: nmset
5 Replies

6. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

7. Shell Programming and Scripting

2 dimensional array in unix

I am trying to implementing two dimensinal array in ksh script.Would you pls help me out. I have a large size of file, File contains looks like ID SID VLAUE1 VALUE2 TOTALVALUE 1 a1 01 02 03 1 b1 02 05 07 ... (2 Replies)
Discussion started by: pritish.sas
2 Replies

8. Shell Programming and Scripting

Manipulating Pick multi dimensional data with awk.

Hi. I am reasonably new to awk, but have done quite a lot of unix scripting in the past. I have resolved the issues below with unix scripting but it runs like a dog. Moved to awk for speed and functionality but running up a big learning curve in a hurry, so hope there is some help here. I... (6 Replies)
Discussion started by: mike.strategis
6 Replies

9. Shell Programming and Scripting

Help for record (2 dimensional array.)

I am going to develop a address book using the shell scripting commands without sed, awk, .... I am thinking to apply the concept of 2 dimenstional array. Can I create a two dimensional array for the insertion/updation/deletion of record in unix. If yes then tell me plz or recommend me some... (1 Reply)
Discussion started by: murtaza
1 Replies

10. Shell Programming and Scripting

Reference two dimensional array in Perl sub

I am trying to reference a two dimensional array in a subroutine and can't seem to figure this one out in Perl. Does anybody know? Please enlighten me. #!/usr/bin/perl -w use constant DIM => 4; sub Shift_elements_right{ my (@Input, @Output) = @_; for ($i = 0 ; $i <= DIM ;... (5 Replies)
Discussion started by: photon
5 Replies
Login or Register to Ask a Question