-1

Find count of a unique values in second column for every unique value in column one using awk associative arrays??

a,x  
a,y  
a,z  
a,w  
b,x  
b,y  
a,x  
b,x

o/p should be

a,4  
b,2
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36

2 Answers2

1

similar awk

$ awk -F, -v OFS=, '{a[$1]+=!b[$1,$2]++} END{for(k in a) print k,a[k]}' file

a,4
b,2

another approach

$ sort -u file | cut -d, -f1 | uniq -c | awk '{print $2","$1}'
karakfa
  • 66,216
  • 7
  • 41
  • 56
0

Input

$ cat file
a,x
a,y
a,z
a,w
b,x
b,y
a,x
b,x

Output

$ awk  'BEGIN{FS=OFS=","}!(($1,$2) in b){b[$1,$2]; a[$1]++}END{for(i in a)print i,a[i]}' file
a,4
b,2

Readable version

awk  'BEGIN{
              FS=OFS=","        # Set input and output separator
           }
!(($1,$2) in b){                # check index col1,col2 exists in array b
              b[$1,$2]          # if not exists then its unique combination
              a[$1]++           # set array b with index col1,col2 and increment count of array a
           }
        END{                    # finally loop through array a and print contents
              for(i in a)
                    print i,a[i]
           }' file
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36