1

This question already has an answer here:

I'm trying to create a new variable array out of the unique values that are in another array but I'm not getting the desired result.

Original Array

# echo ${owner[*]}
390920ad-2858-e651-a4af-a9eaa6acaebb 390920ad-2858-e651-a4af-a9eaa6acaebb e14c2413-7179-44f8-dfc3-b8624dcb10bb 390920ad-2858-e651-a4af-a9eaa6acaebb e14c2413-7179-44f8-dfc3-b8624dcb10bb 390920ad-2858-e651-a4af-a9eaa6acaebb e14c2413-7179-44f8-dfc3-b8624dcb10bb 390920ad-2858-e651-a4af-a9eaa6acaebb 390920ad-2858-e651-a4af-a9eaa6acaebb 390920ad-2858-e651-a4af-a9eaa6acaebb e14c2413-7179-44f8-dfc3-b8624dcb10bb 390920ad-2858-e651-a4af-a9eaa6acaebb e14c2413-7179-44f8-dfc3-b8624dcb10bb 390920ad-2858-e651-a4af-a9eaa6acaebb 390920ad-2858-e651-a4af-a9eaa6acaebb 390920ad-2858-e651-a4af-a9eaa6acaebb 390920ad-2858-e651-a4af-a9eaa6acaebb 0a452389-5ed2-e46f-ad15-cc538c82650d 4232f23d-ed48-4b14-c0ea-aa911fd24920 8ee1b05f-2473-4c37-bfc5-ae393921b939

Command I'm using

uniq=($(printf "%s\n" "${owner[@]}" | sort -u))

Issue (It's storing all the unique values as a single value under index 0)

# echo ${uniq[0]}
0a452389-5ed2-e46f-ad15-cc538c82650d
390920ad-2858-e651-a4af-a9eaa6acaebb
4232f23d-ed48-4b14-c0ea-aa911fd24920
8ee1b05f-2473-4c37-bfc5-ae393921b939
e14c2413-7179-44f8-dfc3-b8624dcb10bb
# echo ${uniq[1]}

#

Anyone know a better way of grabbing the unique values from this array?

Using bash on SmartOS (Similar to Solaris)

EDIT

I've tried the following as well which will store each value under it's own index number but it does not remove the duplicate values:

uniq=($(printf "%s " "${owner[@]}" | sort -u))

marked as duplicate by don_crissti, Stephen Rauch, John WH Smith, countermode, Anthon Jul 12 '17 at 13:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Your results suggest that your shell's IFS variable does not include newline - have you modified IFS? what version of bash is it? – steeldriver Jul 11 '17 at 21:55
  • 1
    Off-topic: Are you using Joyent? (Since you mention SmartOS.) – Wildcard Jul 11 '17 at 21:58
  • Damnit. I am setting the IFS in my script and think I may have forgot to change it back during testing. If I just unset IFS will that revert it to default or do you know how I could? – Jesse_b Jul 11 '17 at 22:00
  • 1
    @Wildcard yes I am. – Jesse_b Jul 11 '17 at 22:00
  • 1
    Cool. For resetting IFS, that's really a separate question, but default value should be restored by IFS=$' \t\n' (That's a space, tab, newline.) – Wildcard Jul 11 '17 at 22:16
1
uniq=($(printf "%s\n" "${owner[@]}" | sort -u | tr '\n' ' '))

Should do it.

Or, as noted in a comment, modify your IFS.

1

Your use of a echo "${owner[*]}" makes that one long string.

You should have used printf '%s\n' "${owner[@]}".

If an space (or a new line) is the delimiter of the strings you pasted in your question, we can recover the original array with (copy and paste from your post)

$ owner=(390920ad-2858-e651-a4af-a9eaa6acaebb 390920ad-2858-e651-a4af-a9eaa6acaebb e14c2413-7179-44f8-dfc3-b8624dcb10bb 390920ad-2858-e651-a4af-a9eaa6acaebb e14c2413-7179-44f8-dfc3-b8624dcb10bb 390920ad-2858-e651-a4af-a9eaa6acaebb e14c2413-7179-44f8-dfc3-b8624dcb10bb 390920ad-2858-e651-a4af-a9eaa6acaebb 390920ad-2858-e651-a4af-a9eaa6acaebb 390920ad-2858-e651-a4af-a9eaa6acaebb e14c2413-7179-44f8-dfc3-b8624dcb10bb 390920ad-2858-e651-a4af-a9eaa6acaebb e14c2413-7179-44f8-dfc3-b8624dcb10bb 390920ad-2858-e651-a4af-a9eaa6acaebb 390920ad-2858-e651-a4af-a9eaa6acaebb 390920ad-2858-e651-a4af-a9eaa6acaebb 390920ad-2858-e651-a4af-a9eaa6acaebb 0a452389-5ed2-e46f-ad15-cc538c82650d 4232f23d-ed48-4b14-c0ea-aa911fd24920 8ee1b05f-2473-4c37-bfc5-ae393921b939)

to be able to print:

$  printf '%s\n' "${owner[@]}"

390920ad-2858-e651-a4af-a9eaa6acaebb
390920ad-2858-e651-a4af-a9eaa6acaebb
e14c2413-7179-44f8-dfc3-b8624dcb10bb
390920ad-2858-e651-a4af-a9eaa6acaebb
e14c2413-7179-44f8-dfc3-b8624dcb10bb
390920ad-2858-e651-a4af-a9eaa6acaebb
e14c2413-7179-44f8-dfc3-b8624dcb10bb
390920ad-2858-e651-a4af-a9eaa6acaebb
390920ad-2858-e651-a4af-a9eaa6acaebb
390920ad-2858-e651-a4af-a9eaa6acaebb
e14c2413-7179-44f8-dfc3-b8624dcb10bb
390920ad-2858-e651-a4af-a9eaa6acaebb
e14c2413-7179-44f8-dfc3-b8624dcb10bb
390920ad-2858-e651-a4af-a9eaa6acaebb
390920ad-2858-e651-a4af-a9eaa6acaebb
390920ad-2858-e651-a4af-a9eaa6acaebb
390920ad-2858-e651-a4af-a9eaa6acaebb
0a452389-5ed2-e46f-ad15-cc538c82650d
4232f23d-ed48-4b14-c0ea-aa911fd24920
8ee1b05f-2473-4c37-bfc5-ae393921b939

And then, filter them as needed:

$ printf '%s\n' "${owner[@]}" | sort -u
0a452389-5ed2-e46f-ad15-cc538c82650d
390920ad-2858-e651-a4af-a9eaa6acaebb
4232f23d-ed48-4b14-c0ea-aa911fd24920
8ee1b05f-2473-4c37-bfc5-ae393921b939
e14c2413-7179-44f8-dfc3-b8624dcb10bb

And to get an array of values in the var uniq, do:

$ uniq=( $(printf '%s\n' "${owner[@]}" | sort -u) )

All the above works because the default IFS is .
If IFS change, the result may also change.

Not the answer you're looking for? Browse other questions tagged or ask your own question.