Вот один в awk (в симпатичной печати, для вашего удовольствия):
$ awk '
BEGIN { # before anything
FS=OFS="," # set the field separators
}
NR>1 { # skip header line, process others
c[$1]++ # count the times a customer_id seen
n[$1]+=$3 # total number of items purchased
}
END { # after processing all transactions
print "customer_id","avg_no_items" # print header
for(i in c) { # loop customer_ids in random order
print i,n[i]/c[i] # compute avg and print
u++ # count uniq customer_ids in c
}
print "Number of unique customer_ids: " u # in GNU awk use length(c) and lose u
}' file
Вывод:
customer_id,avg_no_items
1,63.5
2,56
3,84
Number of unique customer_ids: 3
Здесь в обещанной однострочной форме:
$ awk 'BEGIN{FS=OFS=","}NR>1{c[$1]++;n[$1]+=$3}END{print "customer_id","avg_no_items";for(i in c){print i,n[i]/c[i];u++}print "Number of unique customer_ids: " u}' file