Geroge Geroge - 2 years ago 94
Linux Question

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

Thas 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 \n:


I can do this in separated 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 ?

Answer Source

Looks like you want this:

awk -v search="A" '{a[$1]++} 
                   $2 == search {count++}
                   END{OFS="\n";print count+0, NR, length(a)}' file
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download