Merging 2 Arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging 2 Arrays
# 1  
Old 12-05-2013
Merging 2 Arrays

I am trying to create a script that combines 2 arrays:

Code:
#!/bin/bash
read -a unix    #(a c e g)
read -a test     #(b d f)

#now I want to merge ${unix[@]} with ${test[@]}, one after another such that the result would be: (abcdefg)
#I've tried quite a few options and can't seem to make it work

Last edited by Scrutinizer; 12-05-2013 at 02:14 AM.. Reason: code tags
# 2  
Old 12-05-2013
Code:
#! /bin/bash
read -a a1
read -a a2

len1=${#a1[@]}
len2=${#a2[@]}

[ $len1 -lt $len2 ] && len=$len2 || len=$len1

i=0
while [ $i -lt $len ]
do
    printf "%s %s " ${a1[$i]} ${a2[$i]}
    (( i++ ))
done


Last edited by balajesuri; 12-05-2013 at 04:46 AM..
This User Gave Thanks to balajesuri For This Post:
# 3  
Old 12-05-2013
A different approach would be
Code:
#!/bin/bash

unix=( a c e g )
test=( b d f )

printf "%s\n" ${unix[@]} >/tmp/1
printf "%s\n" ${test[@]} >/tmp/2

result=$( paste /tmp/1 /tmp/2 )
echo ${result[@]}
rm /tmp/1 /tmp/2

or shorter:

Code:
#!/bin/bash

unix=( a c e g )
test=( b d f )

result=$( paste <( printf "%s\n" ${unix[@]} ) <(printf "%s\n" ${test[@]}) )
echo ${result[@]}


Last edited by hergp; 12-05-2013 at 06:19 AM.. Reason: added second version
This User Gave Thanks to hergp For This Post:
# 4  
Old 12-05-2013
As you are using bash, here strings should be available. Try
Code:
sort <<<"$(echo ${unix[@]} ${test[@]}|tr ' ' '\n') "
a
b
c
d 
e
f
g

or, to get it into an array,
Code:
res=( $(sort <<<"$(echo ${unix[@]} ${test[@]}|tr ' ' '\n') ") )


Last edited by RudiC; 12-05-2013 at 05:14 AM.. Reason: left output of set -vx in ... removed
This User Gave Thanks to RudiC For This Post:
# 5  
Old 12-05-2013
I think, that's not what the OP wants. I understood that he/she want's a sequence of unix[0], test[0], unix[1], test[1], etc., but I might be wrong.
This User Gave Thanks to hergp For This Post:
# 6  
Old 12-05-2013
Yes, hergp, that is exactly what I am trying to do
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on merging

I have a total of 100 files (variable size of each file) with total size of 328950 bytes. I want to merge those 100 files into 4 files with each size be close to equal size i.e (328950/4 ~= 82238) but do not want to break any file. Any unix sheel script help will be really helpful. (18 Replies)
Discussion started by: George1234
18 Replies

2. Shell Programming and Scripting

Merging 2 Arrays in a script

I am creating (with help) my own version of a calculator script that simply merges 2 arrays ( ${a}${b}${a}${b}... etc ) #!/bin/bash echo "Enter the integers you would like to calculate" read -a nums echo "You entered ${#nums} integers" let batch="${#nums}-1" echo "Enter how you want to... (2 Replies)
Discussion started by: pbmitch
2 Replies

3. Shell Programming and Scripting

Merging

Hi, I have searched the forums for a solution but I haven't found a perfect answer, and I'm a bit of a novice, so I hope someone can help: I have 2 files: file1: Chr1 139311 1/1:37,3,0:19 Chr1 139350 1/1:67,6,0:19 Chr1 139404 1/1:0,0,0:7 Chr1 152655 0/1:0,0,0:3 Chr1 152718... (2 Replies)
Discussion started by: ljk
2 Replies

4. Shell Programming and Scripting

Merging two files with merging line by line

Hi, I have two files and i want to merge it like, file1.txt --------- abc cde efg file2.txt ------- 111 222 333 Output file should be, -------------- abc 111 (2 Replies)
Discussion started by: rbalaj16
2 Replies

5. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

6. Shell Programming and Scripting

perl : merging two arrays on basis of common parameter

I have 2 arrays, @array1 contains records in the format 1|_|X|_|ssd|_| 4|_|H|_|hbd|_| 9|_|Y|_|u8gjdfg|_| @array2 contains records in the format X|_|asdf|_| Y|_|qwer|_| A|_|9kdkf|_| @array3 should contain records in the PLz X|_|ssd|_|asdf|_| Y|_|hdb|_|qwer|_| PLZ dont use... (2 Replies)
Discussion started by: centurion_13
2 Replies

7. Shell Programming and Scripting

merging

Hi all, I have 2 files. I want to merge a portion or column in file 2 into file 1. file 1 - not tab or space delimited B_1 gihgjfhdj| hgfkddlldjljldjlddl B_2 gihgjddshjgfhs| hgfkddlldjljldjlddl B_3 gihgjfhdj| hgfkddlldjljldjlddlhgjdhdhjdhjhdjhdjhgdj file2 -... (7 Replies)
Discussion started by: Lucky Ali
7 Replies

8. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

9. Shell Programming and Scripting

Merging arrays

Hi all, I need some help in merging arrays. I have two arrays and using korn shell Array1 AB23 AB24 Array2 CD00 CD01 CD02 Elements from array 1 should always alternate with elements of arrays 2 i.e the result should look like AB23CD00 AB24CD01 AB23CD02 Any help is appreciated.... (4 Replies)
Discussion started by: jakSun8
4 Replies

10. Shell Programming and Scripting

Merging Help

Hi Gurus, I need a help in merging the files. I have nearly 7 files and the files will have time stamp in it. I need to merger these files condition is it is not necessary that all the 7 files has to be there. suppose if i have only 3 files availabe out of these 7 then i need to merge... (3 Replies)
Discussion started by: kumarc
3 Replies
Login or Register to Ask a Question