Sorting the contents of a variable


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Sorting the contents of a variable

i want to reduce the OPTIONS variable which is a list of email addresses to just the unique entries. The following works, but, i would like to accomplish it without using the temporary file (dest.txt)



Code:
$echo $OPTIONS                         
a b c A B D                                         
$cat clean                             
#!/usr/bin/bash                                     
n=$#                                                
i=1                                                 
cat /dev/null >/usr/spool/lp/temp/dest.txt          
while [ $i -le $n ]                                 
do                                                  
        c=$(echo $1 |tr "[:upper:]" "[:lower:]" )   
        echo $c>>/usr/spool/lp/temp/dest.txt        
        shift                                       
        let i=$i+1                                  
done                                                
sort -u </usr/spool/lp/temp/dest.txt                
                                                    
$ OPTIONS1=$(./clean $OPTIONS)          
$ echo $OPTIONS1                        
a b c d                                             
$

# 2  
Code:
echo "4 2 d c b 1 a b A b 1 B C a b c d" | ./clean.awk

# output
 a b c d 1 2 4

clean.awk
Code:
#!/usr/bin/awk -f
{
   for(i=1;i<=NF;i++)
      a[tolower($i)]=1
} 

END{ 
   for(i in a)
      printf i " "
   printf "\n"
}

awk seems to have a sorting algorithm when accessing lists.

...or in one line:

Code:
echo "4 2 d c b 1 a b A b 1 B C a b c d" | awk '{for(i=1;i<=NF;i++) {a[tolower($i)]=1}} END{for(i in a) {printf i " "} ; printf "\n"}'

# 3  
How about:

Code:
OPTIONS="a b c A B D"

OPTIONS=$( \
               echo "${OPTIONS}" | \
               tr " " "\n" | \
               tr "[:upper:]" "[:lower:]" | \
               sort -u | \
               tr "\n" " " \
         )

I've just broken up the lines for clarity. Does that do the job?

Not a great plan if the content of $OPTIONS is very large (a few million items) and would be terrible with any values containing a space.

Also, don't call this in a loop because of the many many processes it might create and the costs associated with that.




Robin
# 4  
After a little thought:
Code:
#!/usr/bin/bash                                   
n=$#                                              
i=1                                               
C=""                                              
while [ $i -le $n ]                               
do                                                
        c=$(echo $1 |tr "[:upper:]" "[:lower:]" ) 
        C="$c\n$C"                                
        shift                                     
        let i=$i+1                                
done                                              
echo $C |sort -u

# 5  
How about
Code:
$ echo $(shuf -e ${OPTIONS,,} | sort -u)
 a b c d

(provided your system's bash provides "Parameter Expansion / Case modification" and shuf has the -e option)


EDIT: or

Code:
$ echo $( xargs -n1 -d" " <<< ${OPTIONS,,} | sort -u)
 a b c d


or, of course,

Code:
$ echo $( tr ' ' '\n' <<< ${OPTIONS,,} | sort -u)


Last edited by RudiC; 2 Weeks Ago at 05:27 PM..
This User Gave Thanks to RudiC For This Post:
# 6  
Assuming that $OPTIONS does not expand to a string containing anything other than alphanumeric characters and <space>s, one could also use:
Code:
echo $(echo $OPTIONS|tr ' [:upper:]' '\n[:lower:]'|sort -u)

without worrying about having a shell that supports the case modification parameter expansion or the shuf utility which are both extensions to the standards.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Storing file contents to a variable
am24
Hi All, I was trying a shell script. I was unable to store file contents to a variable in the script. I have tried the below but unable to do it. Input = `cat /path/op.diary` Input = $(<op.diary) I am using ksh shell. I want to store the 'op.diary' file contents to the variable 'Input'...... UNIX for Beginners Questions & Answers
12
UNIX for Beginners Questions & Answers
Folder contents getting appended as strings while redirecting file contents to a variable
vivek d r
Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ...... Shell Programming and Scripting
2
Shell Programming and Scripting
how to grep the contents of a variable..
suri.tyson
Hello All, I've written a script to collect all audit logs files; now i want to see only the files from it which contains certain x tablenames. I've stored all the tablenames in a log file and using it through variable in a script (ex:below) $ more tablenames.log (this is just a samle...... Shell Programming and Scripting
2
Shell Programming and Scripting
unique sort contents of a variable
praveenbvarrier
Hi , I have #echo $var1 #hdisk2 hdisk3 hdisk0 hdisk2 Now I need to remove duplicate entries from this . ie. after sorting it should only have hdisk2 hdisk3 hdisk0 . I can have these values in a array as well . I understand we can use sort -u to remove the duplicates in a...... Shell Programming and Scripting
2
Shell Programming and Scripting
Save contents of a Variable
carl_vieyra
I want to save the contents of a variable to a file. How can that be achieved? I have tried with: echo $varname > textfile.txt but for some reason it does not print anything.... UNIX for Dummies Questions & Answers
1
UNIX for Dummies Questions & Answers