Re: Getting unique elements in a bash array
- From: "Samik R." <samik@xxxxxxxxxxxxx>
- Date: Mon, 18 Sep 2006 13:05:22 -0600
Chris F.A. Johnson wrote:
On 2006-09-18, Samik R. wrote: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).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' ) )
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
.
- Follow-Ups:
- Re: Getting unique elements in a bash array
- From: Chris F.A. Johnson
- Re: Getting unique elements in a bash array
- References:
- Getting unique elements in a bash array
- From: Samik R.
- Re: Getting unique elements in a bash array
- From: Chris F.A. Johnson
- Getting unique elements in a bash array
- Prev by Date: File Comparing - using sed ?
- Next by Date: Re: File Comparing - using sed ?
- Previous by thread: Re: Getting unique elements in a bash array
- Next by thread: Re: Getting unique elements in a bash array
- Index(es):
Relevant Pages
|