How to remove duplicates in C shell Array?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove duplicates in C shell Array?
# 1  
Old 08-26-2016
Question How to remove duplicates in C shell Array?

Please help me on this

My script name is uniqueArray.csh

Code:
#!/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

Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes them far easier to read and preserves multiple spaces for indenting or fixed-width data.

Last edited by rbatte1; 08-26-2016 at 11:59 AM.. Reason: Added CODE tags
# 2  
Old 08-26-2016
What have you tried?
# 3  
Old 08-26-2016
The following works with csh builtins but does not sort.
Code:
#!/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.
Code:
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).
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 08-28-2016
Quote:
Originally Posted by Corona688
What have you tried?
I finally got a script for removing duplicates and sorting in c shell script.
Code:
#!/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
Code:
 
    a b c y z


Moderator's Comments:
Mod Comment Please use code tags! thanks


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

Quote:
Originally Posted by MadeInGermany
The following works with csh builtins but does not sort.
Code:
#!/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.
Code:
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).
Thank you .your script is working.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove duplicates using for loop?

values=(1 2 3 5 4 2 3 1 6 8 3 5 ) #i need the output like this by removing the duplicates 1 2 3 5 4 6 8 #i dont need sorting in my program #plz explain me as simple using for loop #os-ubuntu ,shell=bash (5 Replies)
Discussion started by: Meeran Rizvi
5 Replies

2. Shell Programming and Scripting

awk - Remove duplicates during array build

Greetings Experts, Issue: Within awk script, remove the duplicate occurrences that are space (1 single space character) separated Description: I am processing 2 files using awk and during processing, I am building an array and there are duplicates on this; how can I delete the duplicates... (3 Replies)
Discussion started by: chill3chee
3 Replies

3. Shell Programming and Scripting

Remove duplicates

Hi I have a below file structure. 200,1245,E1,1,E1,,7611068,KWH,30, ,,,,,,,, 200,1245,E1,1,E1,,7611070,KWH,30, ,,,,,,,, 300,20140223,0.001,0.001,0.001,0.001,0.001 300,20140224,0.001,0.001,0.001,0.001,0.001 300,20140225,0.001,0.001,0.001,0.001,0.001 300,20140226,0.001,0.001,0.001,0.001,0.001... (1 Reply)
Discussion started by: tejashavele
1 Replies

4. Shell Programming and Scripting

Sort and Remove duplicates

Here is my task : I need to sort two input files and remove duplicates in the output files : Sort by 13 characters from 97 Ascending Sort by 1 characters from 96 Ascending If duplicates are found retain the first value in the file the input files are variable length, convert... (4 Replies)
Discussion started by: ysvsr1
4 Replies

5. Shell Programming and Scripting

Remove duplicates

I have a file with the following format: fields seperated by "|" title1|something class|long...content1|keys title2|somhing class|log...content1|kes title1|sothing class|lon...content1|kes title3|shing cls|log...content1|ks I want to remove all duplicates with the same "title field"(the... (3 Replies)
Discussion started by: dtdt
3 Replies

6. Shell Programming and Scripting

Help with merge and remove duplicates

Hi all, I need some help to remove duplicates from a file before merging. I have got 2 files: file1 has data in format 4300 23456 4301 2357 the 4 byte values on the right hand side is uniq, and are not repeated anywhere in the file file 2 has data in same format but is not in... (10 Replies)
Discussion started by: roy121
10 Replies

7. Shell Programming and Scripting

Script to remove duplicates

Hi I need a script that removes the duplicate records and write it to a new file for example I have a file named test.txt and it looks like abcd.23 abcd.24 abcd.25 qwer.25 qwer.26 qwer.98 I want to pick only $1 and compare with the next record and the output should be abcd.23... (6 Replies)
Discussion started by: antointoronto
6 Replies

8. Shell Programming and Scripting

Shell script to remove duplicates lines in a file

Hi, I am writing a shell script that needs to remove duplicate lines within a file by category. example: section a a c b a section b a b a c I need to remove the duplicates within th category with out removing the duplicates from the 2 different sections (one of the a's in section... (1 Reply)
Discussion started by: RichElks
1 Replies

9. Shell Programming and Scripting

Remove duplicates

Hello Experts, I have two files named old and new. Below are my example files. I need to compare and print the records that only exist in my new file. I tried the below awk script, this script works perfectly well if the records have exact match, the issue I have is my old file has got extra... (4 Replies)
Discussion started by: forumthreads
4 Replies

10. Shell Programming and Scripting

Arranging an array so that duplicates will turn up first

Hi All, I have an array that contains duplicates as well unique numbers. ex- (21, 33, 35, 21, 33, 70, 33, 35, 50) I need to arrange it in such a way that all the duplicates will come up first followed by unique numbers. Result for the given example should be: (21, 21, 33, 33, 35, 35, 70,... (4 Replies)
Discussion started by: ashim
4 Replies
Login or Register to Ask a Question