0

it would be appreciated if you can help me out this case.

I have 2 arrays

array1=(1 2 3)
array2=(5 2 6)

Is there anyway to filter out the different elements from comparing those 2 arrays with bash script.

The expected result is

array3=(1 3 5 6)

Thank you so much,

1 Answers1

0

To get the unique elements from an array in bash, you can use this approach:

$ a=(aa ac aa ad)
$ declare -A b
$ for i in ${a[@]}; do b[$i]=1; done
$ echo ${!b[@]}
ac aa ad

The rest is left as an exercice...

Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • Thank you for your suggestion Fredrik Pihl, but your idea is not caught my case. I would like to compare array1 with array2 then filter out the elements that are not duplicated and put them in array3. Do you have any other idea? – user3205709 Jan 21 '14 at 02:27