Getting Distinct values from second field in a file....

Hi

I have a pipe delimited file. I am trying to grab the DISTINCT value from the second field. The file is something like:

1233|apple|ron
1234|apple|elephant
1235|egg|man

the output I am trying to get from second field is apple,egg (apple coming only once)

Thanks
simi

cat myfile | cut -d"|" -f2 | sort | uniq -u

display file
cut the 2nd field
sort
give unique "only" values

nawk -F'|' '!($2 in a) {key[++key[0]]=$2; a[$2]} END {for(i=1;i<=key[0];i++) printf("%s%c", key, (i==key[0])?ORS:OFS)}' OFS=, myFile

Another one :

awk -F"|" ' { arr[$2]; } END { for (i in arr) print i; }'  filename

Sort can do also unique

cut -d"|" -f2 myfile | sort -u

---------- Post updated at 04:35 PM ---------- Previous update was at 04:17 PM ----------

Ksh93-version using associative arrays, same idea as using awk.

#!/bin/ksh
#script
typeset -A values
oifs="$IFS"
while read line
do
    IFS="|"
    set -A flds $line
    IFS="$oifs"
    values["${flds[1]}"]=1
done
# 
echo "${!values[@]}"  
# or
a="${!values[@]}"  
# comma separated
echo "${a// /,}"

./script < infile