479 shaares
1 résultat
taggé
bash
echo -e 'b Y\na X\na Y\na Y\nb Z\na Y\nb Z'
b Y
a X
a Y
a Y
b Z
a Y
b Z
We want to aggregate the second row given the first, counting and showing the different value and the total number of value :
echo -e 'b Y\na X\na Y\na Y\nb Z\na Y\nb Z'|awk -F " " '{count[$1]++; countb[$1$2]++ ; a[$1]=a[$1]?a[$1] OFS $2:$2}END{ for (i in a) {split(a[i],elts,",") ; delete c ; for (j in elts) {c[elts[j]]=1 } ; printf i FS ; s="" ; for (k in c) s=s""k"("countb[i""k]")," ; printf substr(s,1,length(s)-1) ; print " "count[i] }}' OFS=,
a X(1),Y(3) 4
b Y(1),Z(2) 3
This reads : we have 4 lines with "A" as first field, 3 of them have "Y" as a second field.
--Quick-- and dirty but just works
You can also sort on last column by adding :
| sort -nk3