Within a bash script, I am building two dynamic arrays. And I want to construct another array with difference of two. Example:array1=(aa-1234|safsf|sfsfs aa-2345|sfsfs|n0sfm aa-0890|ssfsf|sfss aa-8097|fsfsf|fsfs bb-1234|xvxx|xvxv cc-1234|jmsf|sfsfs cc-1235|xvxv|xvxv)array2=(aa-1234 aa-8097)array1 elements are long entries with | as a delimiter in each.Now I want to construct an array3, whose elements are all of array1 except those matching in array2.I tried  a for loop to skip through elements. However array1 being a long one, it too much time. I'm looking for a more efficient way.
Bash Array Comparision
bash;array
I would construct a regular expression out of the 2nd array, and then compare the elements of the 1st array against it:$ re=$(IFS='|'; echo ^(${array2[*]})\|)$ echo $re^(aa-1234|aa-8097)\|$ for elem in ${array1[@]}; do if [[ $elem =~ $re ]]; then array3+=($elem); fi; done$ printf %s\n ${array3[@]}aa-1234|safsf|sfsfsaa-8097|fsfsf|fsfsIf the arrays are much larger than shown, I'd farm the exercise out to grep$ grep -Ff <(printf %s\n ${array2[@]}) <(printf %s\n ${array1[@]})aa-1234|safsf|sfsfsaa-8097|fsfsf|fsfs