Artificial intelligent assistant

I want to compare two list and print the difference output from list one I have two variables input: VAR1="abc red blue cat empty dummy rummy" VAR2="rummy zero empty rat cat reverse" output: I want output as follows: (remove the common ones from `$VAR2` in `$VAR1`) abc red blue dummy I tried as follows: for i in $VAR1 do for j in $VAR2 do if [ $i != $j ]; then echo $i; fi done done Here, `if [ $i == $j ]; then echo $i`, here I get output perfectly as `cat empty rummy` But I need to get output other than these.

Try:


a=()
for i in $VAR1; do
found=
for j in $VAR2; do
if [ $i == $j ]; then
found=1
break
fi
done
if [ ! $found ]; then
a+=($i)
fi
done
echo ${a[*]}


In words: For every `i` in `VAR1`, compare it against every `j` in `VAR2`. If no match is found, add `i` to the output.

This version is assuming the characters in `VAR1` and `VAR2` won't be confusing the shell. Also, it is inefficient running in quadratic time, but maybe that's not a concern.

Faster, using associative arrays:


declare -A a2
for k in $VAR2; do
a2[$k]=1
done
for k in $VAR1; do
if [ ! "${a2[$k]}" ]; then
echo $k
fi
done

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 45c9367c5d267765b11c683030468a01