简体   繁体   中英

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 ?

Looks like you want this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM