Re: Getting unique elements in a bash array



Chris F.A. Johnson wrote:
On 2006-09-18, Samik R. wrote:
Hello,

I am trying to obtain the unique elements (numbers) in a bash array. As of now, I am doing this:

declare -a array1=`echo ${array[@]}|awk '{for(i=1;i<NF;i++) printf "%d\n",$i}'|sort|uniq`

Ideally, I would like to modify 'array' itself with the result, and would like to do it more elegantly. My attempts like:

array=`echo ${array[@]}|awk '{for(i=1;i<NF;i++) printf "%d\n",$i}'|sort|uniq`

did not help. Would like to have some pointers.

Assuming there are no newlines in any of the array elements:

IFS='
'
array=( $( printf "%s\n" "${array[@]}" | awk 'x[$0]++ == 0' ) )

Thanks Chris. Couple of questions: why is the setting of IFS required? In my cygwin bash shell, my IFS is defined as $' \t\n', and the next line works fine without the IFS setting (i.e., running echo ${array[@]} results in printing the unique elements on the same line, separated by a space).

Also, is there a difference between setting IFS as what you did and setting IFS='\n'? If I set the IFS to '\n', run the next line, and then echo ${array[@]}, each element appears in a line.

Lastly, can you decode in a few lines what the awk part is doing? It looks advanced to me !! I have done till this much .. :-)
- awk 'x[$0]++ == 0'
- awk parses each line of the input (obtained from $0), put it in an array x ... ??

Thanks.
-Samik
.



Relevant Pages