0

I fairly new to bash programming - sorry if my terminology is incorrect

I have two lists stored as strings

echo [$LISTA]
["a1ex","oliver","maggie","walter","ben"]
echo $LISTB
[ "a1ex", "oliver", "ben"]

I want to get all items in LISTA that is not in LISTB

"maggie", "walter"

I found this other SO post that compares the two arrays in bash. I can do it that way but I just have to transform my strings to arrays. I am wondering is there a way to do it without transforming the strings to array?

OLIVER.KOO
  • 5,654
  • 3
  • 30
  • 62
  • As below, it is a high price in process count to avoid using arrays. If you're trying to be efficient, converting to, (or creating your lists to begin with as) arrays, will be the most eficient solution (IMHO!). Good luck. – shellter Jul 07 '20 at 00:05

2 Answers2

3
jq -r '.[]' <<<'["a1ex","oliver","maggie","walter","ben"]' |
  fgrep -vF "$(jq -r '.[]' <<<'[ "a1ex", "oliver", "ben" ]')" -- -

Output:

maggie
walter
Léa Gris
  • 17,497
  • 4
  • 32
  • 41
0

try this

$ f() { echo "$1" | tr '[,]' '\n' | sort; }; comm -23 <(f "$listA") <(f "$listB") | 
  paste -ds,

may not handle arbitrary white space...

karakfa
  • 66,216
  • 7
  • 41
  • 56