Geroge
Geroge

Reputation: 561

awk print number of row only in uniq column

I have data set like this:

1 A
1 B
1 C
2 A
2 B
2 C
3 B
3 C

And I have a script which calculates me:

  1. Number of occurrences in searching string

  2. Number of rows

    awk -v search="A" \
       'BEGIN{count=0} $2 == search {count++} END{print count "\n" NR}' input
    

That works perfectly fine.

I would like to add to my awk one liner number of unique lines from the first column.

So the output should be separated by \n:

2
8
3

I can do this in separate awk code, but I am not able to integrate it to my original awk code.

awk '{a[$1]++}END{for(i in a){print i}}' input  | wc -l 

Any idea how to integrate it in one awk solution without piping ?

Upvotes: 1

Views: 290

Answers (1)

hek2mgl
hek2mgl

Reputation: 157967

Looks like you want this:

awk -v search="A" '{a[$1]++} 
                   $2 == search {count++}
                   END{OFS="\n";print count+0, NR, length(a)}' file

Upvotes: 4

Related Questions