I've got almost the same question as here.
I have an array which contains aa ab aa ac aa ad
, etc.
Now I want to select all unique elements from this array.
Thought, this would be simple with sort | uniq
or with sort -u
as they mentioned in that other question, but nothing changed in the array...
The code is:
echo `echo "${ids[@]}" | sort | uniq`
What am I doing wrong?
'sort' can be used to order the output of a for-loop:
and eliminate duplicates with "-u":
Finally you can just overwrite your array with the unique elements:
this one will also preserve order:
and to modify the original array with the unique values:
Without loosing the original ordering:
If your array elements have white space or any other shell special character (and can you be sure they don't?) then to capture those first of all (and you should just always do this) express your array in double quotes! e.g.
"${a[@]}"
. Bash will literally interpret this as "each array element in a separate argument". Within bash this simply always works, always.Then, to get a sorted (and unique) array, we have to convert it to a format sort understands and be able to convert it back into bash array elements. This is the best I've come up with:
Unfortunately, this fails in the special case of the empty array, turning the empty array into an array of 1 empty element (because printf had 0 arguments but still prints as though it had one empty argument - see explanation). So you have to catch that in an if or something.
Explanation: The %q format for printf "shell escapes" the printed argument, in just such a way as bash can recover in something like eval! Because each element is printed shell escaped on it's own line, the only separator between elements is the newline, and the array assignment takes each line as an element, parsing the escaped values into literal text.
e.g.
The eval is necessary to strip the escaping off each value going back into the array.
A bit hacky, but this should do it:
To save the sorted unique results back into an array, do Array assignment:
If your shell supports herestrings (
bash
should), you can spare anecho
process by altering it to:Input:
Output:
Explanation:
"${ids[@]}"
- Syntax for working with shell arrays, whether used as part ofecho
or a herestring. The@
part means "all elements in the array"tr ' ' '\n'
- Convert all spaces to newlines. Because your array is seen by shell as elements on a single line, separated by spaces; and because sort expects input to be on separate lines.sort -u
- sort and retain only unique elementstr '\n' ' '
- convert the newlines we added in earlier back to spaces.$(...)
- Command Subsitutiontr ' ' '\n' <<< "${ids[@]}"
is a more efficient way of doing:echo "${ids[@]}" | tr ' ' '\n'