unique storage structure in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting unique storage structure in ksh
# 1  
Old 09-15-2009
unique storage structure in ksh

hi all,
is there a storage structure in ksh which only allows unique values i.e overwrites duplicates values ??

am using an array to hold server names from a 'find' command but some of the names are repeated. Is there an easy way to remove the duplicates ??


ta.
# 2  
Old 09-15-2009
try..

Code:
find *some* | uniq

# 3  
Old 09-16-2009
Quote:
Originally Posted by ryandegreat25
try..

Code:
find *some* | uniq

Just a short note - "uniq" works on sorted output. Unless that "find" command returns sorted output, uniq will not work.

Code:
$ 
$ # find all server names
$ find . -name "*" -type f
./s3
./d1/s1
./d2/s3
./s1
./s2
$ 
$ # just the server names; without path
$ find . -name "*" -type f | sed 's/.*\///'
s3
s1
s3
s1
s2
$ 
$ # unsorted output - uniq does not work on this
$ find . -name "*" -type f | sed 's/.*\///' | uniq
s3
s1
s3
s1
s2
$ 
$ # now sort the server names and pipe to uniq
$ find . -name "*" -type f | sed 's/.*\///' | sort | uniq
s1
s2
s3
$

@OP - the data structure that stores unique values is called an "associative array" or "hash". The keys of a hash cannot repeat, hence the server names could be the keys. The value could be a constant or a number that is incremented per occurrence.
Check "man ksh" or "typeset" command for associative arrays in Korn shell.
Most of the scripting languages provide such a data structure - it is called "associative array" in awk, "hash" in Perl and Ruby and "dictionary" (not completely sure) in Python.
In any case, as suggested by ryandegreat25, de-duping your server names before storing them in an array would be the way to go.

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating array with non-duplicate / unique elements in ksh

Hi all, I have created 3 arrays which can have common elements in each like- arr_a contains str1 str2 str3 str4 str5 arr_b contains str3 str6 str7 str1 str8 arr_c contains str4 str9 str10 str2 each array is created with "set -A arr_name values" command. I want to create a resultant array-say... (1 Reply)
Discussion started by: sanzee007
1 Replies

2. Shell Programming and Scripting

Perl Data Structure - Non unique values

I have the perl data structure and what i need to do is find all values in @{$extractColumns{'2'}{'D'}} which are not there in @{$extractColumns{'2'}{'M'}} but seems like i need to put a flag somewhere and i messed up foreach my $order (keys %extractColumns) { foreach my $value... (2 Replies)
Discussion started by: dinjo_jo
2 Replies

3. UNIX for Dummies Questions & Answers

ksh case structure

Hello Experts, I ve been trying to build another shell where I am using the following code. transact="tv5cpc1" case "$transact" in "...cp..") xActType="" ;; "...de..") xActType="sp_dep" ;; "...ep..") xActType="sp_epa" ;; "....v.") ... (4 Replies)
Discussion started by: hkansal
4 Replies

4. Shell Programming and Scripting

ksh scripting: Extract 1 most recent record for unique key

I'm loading multiple delimited files into an Oracle DB using sqlldr on Unix. I would like to get only the most recent record per each unique key. There may be multiple updates for each key, but I only want the most recent one. There is a date column in my delimited files, so I'm using cat to... (2 Replies)
Discussion started by: OPTIMUS_prime
2 Replies

5. Programming

Search attributes in one structure using the values from another structure

Hello Groups I am trying to find out ways of comparing a value from a 'c' structure to a value in another 'C' structure. the 'C' structure can be a List or liked list as it contains lot many records. if we loop it in both the structures it is going to consume time. I am looking for a simple... (3 Replies)
Discussion started by: dhanamurthy
3 Replies

6. UNIX for Dummies Questions & Answers

Copying a Directory Structure to a new structure

Hi all Is it possible to copy a structure of a directory only. e.g. I have a file with the following entries that is a result of a find :- /dir1/dir2/file.dbf /dir1/dir2/dir3/file1.dbf /dir1/file.dbf I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Discussion started by: jhansrod
8 Replies
Login or Register to Ask a Question