0

I have 2 arrays defined below.

Arr1

"name": "key1", "value": "value1"
"name": "key2", "value": "value2"
"name": "key3", "value": "value3"

Arr2

value1=/other/path/to/file1
value3=/other/path/to/file3

I want to map these 2 arrays in such a way that my resulting array must look like below.

Output Array

"name": "key1", "value": "/other/path/to/file1"
"name": "key2", "value": "value2"
"name": "key3", "value": "/other/path/to/file3"

So basically I need to write some command in my bash script which will do such mapping and provide me required output.

Output of declare -p Arr1 Arr2 is:

declare -p arr1 arr2

declare -a arr1='([0]="name:" [1]="key1," [2]="value:" [3]="value1" [4]="name:" [5]="key2," [6]="value:" [7]="value2" [8]="name:" [9]="key3," [10]="value:" [11]="value3")'
declare -a arr2='([0]="value1=/other/path/to/file1" [1]="value3=/other/path/to/file3")'
10
  • 2
    Show us the code form of the bash arrays you've maintained Commented Jul 14, 2017 at 9:42
  • You say that you have arrays but really, these are just strings, as far as bash is concerned. Can we assume that they're in two separate files? Are the keys and values guaranteed not to contain quotes and colons? Commented Jul 14, 2017 at 9:43
  • @TomFenech quotes and columns are all present as given in the example here. I have corrected the arrays. Each line of the above arrays represent a single element of that array. Commented Jul 14, 2017 at 9:49
  • We still don't know if these are lines in a script (Arr2 looks like variable assignments in a shell script now), or lines in a text file, or what. Commented Jul 14, 2017 at 9:53
  • @TomFenech As I clearly said in my previous comment, each line denote an element of the array. So Arr2[0] is 'value1=/other/path/to/file1' and similarly Arr2[1] is 'value3=/other/path/to/file3'. Hope this clears your confusion now. Commented Jul 14, 2017 at 9:57

2 Answers 2

1

Script.sh

array1=( '"name": "key1", "value": "value1"'
'"name": "key2", "value": "value2"'
'"name": "key3", "value": "value3"' )

array2=( 'value1=/other/path/to/file1' 'value3=/other/path/to/file3' )

for element in "${array2[@]}"
do
    key=`echo $element | cut -d '=' -f1`
    value=`echo $element | cut -d '=' -f2-`
    i=0
    for elem in "${array1[@]}"
    do
        array1[i]=`echo $elem | sed -e "s#$key#$value#"`
        (( i++ ))
    done
done

for element in "${array1[@]}"
do
   echo $element
done

Result:

./test.sh 
"name": "key1", "value": "/other/path/to/file1"
"name": "key2", "value": "value2"
"name": "key3", "value": "/other/path/to/file3"
Sign up to request clarification or add additional context in comments.

2 Comments

This would only give what is present in second array mapping with first array. I hav edited the output array to give me exact result. Please have a look at updated value and see if you could help.
Added the output that I got. Can you please mention the change in this output.
1

You can use this script in bash:

declare -a arr1='([0]="name:" [1]="key1," [2]="value:" [3]="value1" [4]="name:" [5]="key2," [6]="value:" [7]="value2" [8]="name:" [9]="key3," [10]="value:" [11]="value3")'
declare -a arr2='([0]="value1=/other/path/to/file1" [1]="value3=/other/path/to/file3")'

arr=()

for i in "${arr1[@]}"; do
   s="$i"
   if [[ $i =~ ^value[0-9]+$ ]]; then
      for j in "${arr2[@]}"; do
         if [[ $j == $i"="* ]]; then
            s="${j#$i=}"
            break
         fi
      done
   fi
   arr+=("$s")
done

# print formatted output
#declare -p arr
for i in "${arr[@]}"; do
   printf "%s" "$i"
   ((++n % 4 == 0)) && printf "\n" || printf " "
done

Output:

name: key1, value: /other/path/to/file1
name: key2, value: value2
name: key3, value: /other/path/to/file3

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.