3

I've got two lists:

tagged=(a-01 a-02 a-03 a-04 a-06)
merged=(a-01 a-02 a-05)

I'm looking for a bash solution to print only those elements which are in tagged list but are not in merged ((a-03 a-04 a-06)).

I've been trying to iterate over both of those list and I failed. I strongly believe that bash got some sneaky way to solve it.

Kacper
  • 1,182
  • 1
  • 8
  • 20

1 Answers1

5

You can use grep -vf with process substitution:

tagged=(a-01 a-02 a-03 a-04 a-06)
merged=(a-01 a-02 a-05)

grep -vf <(printf "%s\n" "${merged[@]}") <(printf "%s\n" "${tagged[@]}")

a-03
a-04
a-06

To store the results in an array:

diffarr=($(grep -vf <(printf "%s\n" "${merged[@]}") <(printf "%s\n" "${tagged[@]}")))

declare -p diffarr
declare -a diffarr='([0]="a-03" [1]="a-04" [2]="a-06")'
123
  • 10,183
  • 2
  • 20
  • 41
anubhava
  • 713,503
  • 59
  • 514
  • 593