user1719051
user1719051

Reputation: 109

Counting occurence with a condition using awk

Say for dataset

Jack apple
Jack pear
Annie apple
Olivia grapes
Olivia pear
Olivia apple
Jack pear
Jack apple

I need for each fruit how many times did a person buy it. For eg the final output should be

apple, Annie, 1
apple, Jack, 2
apple, Olivia, 1
grapes, Olivia, 1
pear, Jack, 2
pear, Olivia, 1 

I don't know how many different types of fruit or how many persons. I am able to get the result for a specific fruit but need help to include all types of fruit

awk '{
all[$1]++
if ($2=="apple") appcount[$1]++} END {for (user in all) print user,  appcount[user]}                                         

'

Upvotes: 2

Views: 63

Answers (3)

jaypal singh
jaypal singh

Reputation: 77085

An all-in-one perl alternate:

perl -lane '$h{$F[1],", ",$F[0]}++ }{ print join ", ", $_, $h{$_} for sort keys %h' file
apple, Annie, 1
apple, Jack, 2
apple, Olivia, 1
grapes, Olivia, 1
pear, Jack, 2
pear, Olivia, 1

Upvotes: 1

karakfa
karakfa

Reputation: 67467

using sort assisted awk

$ sort -k2 -k1,1 names | uniq -c | awk -v OFS=", " '{print $3,$2,$1}'
apple, Annie, 1
apple, Jack, 2
apple, Olivia, 1
grapes, Olivia, 1
pear, Jack, 2
pear, Olivia, 1

Upvotes: 2

anubhava
anubhava

Reputation: 784998

Using awk you can do:

awk -v OFS=', ' '{seen[$2 OFS $1]++} END{for (i in seen) print i, seen[i]}' file
apple, Olivia, 1
grapes, Olivia, 1
pear, Olivia, 1
apple, Jack, 2
apple, Annie, 1
pear, Jack, 2

And to get sorted results:

awk -v OFS=', ' '{seen[$2 OFS $1]++} END{for (i in seen) print i, seen[i]}' file |
> sort -t, -k1
apple, Annie, 1
apple, Jack, 2
apple, Olivia, 1
grapes, Olivia, 1
pear, Jack, 2
pear, Olivia, 1

Upvotes: 3

Related Questions