How to remove duplicates in C shell Array?

Please help me on this

My script name is uniqueArray.csh

#!/bin/csh
set ARRAY = ( one teo three one three )
set ARRAY = ( $ARRAY one five three five  )

How to remove the duplicates in this array ,sort and save those in the same variable or different variable.

Thanks in the advance

What have you tried?

The following works with csh builtins but does not sort.

#!/bin/csh -f
set ARRAY=( one two three one three )
echo "<$ARRAY>"
set NARRAY=( )
foreach i ( $ARRAY:q )
  if ( " $NARRAY " !~ *" $i "* ) then
    set NARRAY=( $NARRAY:q $i:q )
  endif
end
set ARRAY=( $NARRAY:q )
echo "<$ARRAY>"

--
While testing I saw a(nother) tcsh bug: * and ? cannot be escaped in the =~ and !~ operators.

if ( x =~ ? ) echo true
if ( x =~ "?" ) echo true
if ( x =~ \? ) echo true

Only the first one should say true.
The bug is in tcsh 6.14 until the current 6.18 (not in 6.13 and earlier).

1 Like

I finally got a script for removing duplicates and sorting in c shell script.

#!/bin/csh
set my_array = ( y z a b a a c )
set my_array = `echo $my_array | sed 's/ /\n/g' | sort -u`
echo $my_array

my output is

 
    a b c y z

---------- Post updated at 04:14 PM ---------- Previous update was at 04:00 PM ----------

Thank you .your script is working.:slight_smile: