Need help in creating arrays using shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in creating arrays using shell
# 1  
Old 06-24-2011
Need help in creating arrays using shell

Hi,

I need help in creating a array in shell scirpt.
I have a file which has following details.
Code:
hostname devices
device1
device 2
de
abcdmhs10 1234
2343
2353
3343
3435
2343
bcdfmhs11 2343
2443
3434
8874
0343
3434
efghnhy001 1234
4687
5673
5678
9857
3434

Above is my test file. I need to create a array in which should be following format.
Code:
array[abcdmhs10]=1234,2343,2353,3343,3435,2343
array [bcdfmhs11]=1234,4687,5673,5678,9857,3434

all the 4 digit values 1234,2343,2353,3343,3435,2343 belong to same host until the next host name in the starting of the line.EG in my eg file abcdmhs10,bcdfmhs11 are the host names and
1234,2343,2353,3343,3435,2343 are the luns that this host has been assigned.
It is not that host name are just [a-zA-Z] they may contain "_" as well.

Now I want to create a file or data structure such a way that i should be able to access all the devices assigned to host.
I am trying with array's in awk. I couldn't exactly figure host to get the all the devices in one line.

Please help me out. Thanks in advance.

Last edited by Franklin52; 06-24-2011 at 03:28 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 06-24-2011
Something like this?
Code:
awk 'NF==2{h=$1; array[h]=$2; next}{array[h]=array[h] "," $1} END{for(i in array)print i "=" array[i]}' file

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 06-24-2011
Have you tried to use pearl read file, and then parse it with awk?
# 4  
Old 06-24-2011
Thanks a lot
Code:
awk 'NF==2{h=$1; array[h]=$2; next}{array[h]=array[h] "," $1} END{for(i in array)print i "=" array[i]}'

This did really worked. I didn't know we do all these things in awk. I was planning to write in perl. thanks a lot.

Last edited by Franklin52; 06-27-2011 at 10:08 AM.. Reason: Please use code tags for code and data samples, thank you
# 5  
Old 06-24-2011
Associative arrays are supported in some shells: ksh93, bash >=4.0, zsh
Code:
typeset -A array   # ksh and zsh
# declare -A array  # bash
index=""
str=""
while read value value2
do
        # new block ? - save previous block
        [ "$value2" != "" -a "$index" != "" ] && array[$index]="$str"  && str=""

        [ "$value2" != "" ] && index="$value" && str="$value2" && continue     # 1st line 
        str="$str,$value"
done < somefile
# save last block
[ "$str" != "" ] && array[$index]="$str"

#
#-number of values
echo "${#array[@]}"
#-keys
echo "${!array[@]}"

echo "bcdfmhs11: ${array[bcdfmhs11]}"

for key in ${!array[@]}
do
        echo "$key - ${array[$key]}"
done

# 6  
Old 06-27-2011
Need clarification.

Quote:
Originally Posted by kshji
Associative arrays are supported in some shells: ksh93, bash >=4.0, zsh
Code:
typeset -A array   # ksh and zsh
# declare -A array  # bash
index=""
str=""
while read value value2
do
        # new block ? - save previous block
        [ "$value2" != "" -a "$index" != "" ] && array[$index]="$str"  && str=""

        [ "$value2" != "" ] && index="$value" && str="$value2" && continue     # 1st line 
        str="$str,$value"
done < somefile
# save last block
[ "$str" != "" ] && array[$index]="$str"

#
#-number of values
echo "${#array[@]}"
#-keys
echo "${!array[@]}"

echo "bcdfmhs11: ${array[bcdfmhs11]}"

for key in ${!array[@]}
do
        echo "$key - ${array[$key]}"
done

I am sorry , I am new to awk programming. I didn't understand what you are doing in the script using associative arrays. Can you write it in bash with comments, it will really help me. I actucally need to perform some other operation with this data.

I need to find out the sizes of each lun for each host.
hostname devices FA0 size
device FA07C:1 69053
device FA07C:1 34536
device FA07C:1 34526
abcdmhs10 1234 FA08C:0 69053
2343 FA08C:0 34536
2353 FA08C:0 34526
3343 FA08C:0 69053
3435 FA08C:0 34536
2343 FA08C:0 34526
bcdfmhs11 2343 FA09A:1 69053
2443 FA09A:1 34536
3434 FA09A:1 34526
8874 FA09A:1 69053
0343 FA09A:1 34536
3434 FA09A:1 34526
efghnhy001 1234 FA06B:0 69053
4687 FA06B:0 34536
5673 FA06B:0 34526
5678 FA06B:0 69053
9857 FA06B:0 34536
3434 FA06B:0 34526
This should be my final o/p.

and My input file is the below format.

hostname devices FA0
device
device
device
abcdmhs10 1234 FA08C:0
2343
2353
3343
3435
2343
bcdfmhs11 2343 FA09A:1
2443
3434
8874
0343
3434
efghnhy001 1234 FA06B:0
4687
5673
5678
9857
3434


Now i need to find out the host and all the devices that are masked to that host and size of each device for that host.
i can find out the size of the each lun using some symmetrix command.

but find out each and every lun would take more time i was wondering if I could run one command
symdev -sid aaaa|egrep "1234|2343|2353|3343|3435|2343" will give the lun sizes in $4 column. I need to add that column value to o/p and my final o/p should be in the above format.

Any pointers is greatly appreciated. thanks in advance.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating script with multiple job arrays

Hello everyone, First of all this is my first post and im fairly new to working with Unix and creating scripts etc. so there will probably be wrong phrases used. Lets get to my questions. I have multiple scripts that submit Slurms/Jobs to the cluster starting like this and doing certain... (3 Replies)
Discussion started by: idbemad
3 Replies

2. Shell Programming and Scripting

Shell arrays need help

Ok so spaces separate elements. What if you wanted an element to have a space in it? For instance: nums="one two three and a half" where "three and a half" is THE SAME element? (3 Replies)
Discussion started by: stevenswj
3 Replies

3. Shell Programming and Scripting

Using arrays in shell

I have three arrays. One is Master array and that has list of other array in config file. for e.g (for simplicity I have only defined array with 2 elements each) set +A MASTERARRAY SQLUPDATE_ONETIME SQLUPDATE_DAILY END_OF_ARRAY set +A SQLUPDATE_ONETIME update12 update22 END_OF_ARRAY... (4 Replies)
Discussion started by: anish
4 Replies

4. Shell Programming and Scripting

arrays in C shell

hi guys, i have the following code in C shell.. set i=0 while ($i < 11) master_array=${ARRAY} i++ done it gives me error at line 3: Variable syntax. what is wrong here? any help is appreciated. (4 Replies)
Discussion started by: npatwardhan
4 Replies

5. Shell Programming and Scripting

I need help with arrays in C Shell

Hi guys could you please post links that explain how to use and manipulate arrays in c shell (.csh files) ? examples are useful too :rolleyes: (5 Replies)
Discussion started by: domain
5 Replies

6. Shell Programming and Scripting

C shell arrays

how do you declare an array in the C shell and loop through each element? (2 Replies)
Discussion started by: npatwardhan
2 Replies

7. Shell Programming and Scripting

Joining two arrays and then creating a variable

Hello all... I'd like to create a variable from an array element from two arrays. In my search for answers I found this code for bash that joins two arrays and then started to work with it. I had got to work once and then foolishly without saving the code, I started to edit it for ksh and... (4 Replies)
Discussion started by: carlos25
4 Replies

8. Shell Programming and Scripting

Creating an unknown number of arrays

I need to create arrays like this: cnt=0 { while read myline; do if ];then firstpass="${myline##<meas>}" meas="${firstpass%%</meas>}" tempmeas="${meas%%;*}" MEAS$cnt=$tempmeas print $cnt print ${MEAS'$cnt'} ... (2 Replies)
Discussion started by: ajgwin
2 Replies

9. Shell Programming and Scripting

how to use arrays in c shell

hi :) i need help to explain arrays 2D in c shell like this in c++ int a (6 Replies)
Discussion started by: hgphsf
6 Replies

10. Shell Programming and Scripting

shell: creating different arrays based on function argument

hi, I was wondering if there was a good way to create an array within a function, where the name is based on a passed argument? I tried this: _____________________________ func(){ #take in 1st arg as the arrayname arrayName=$1 let i=0 while read line do arrayName=${line} let i+=1... (5 Replies)
Discussion started by: nix21
5 Replies
Login or Register to Ask a Question