0

Bash Gurus, I need to compute the max and percentile numbers for each item in the list, using awk

aa  1
ab  3
aa  4
ac  5
aa  3
ad  2
ab  4
ac  2
ae  2
ac  5

Expected output

Item   90th percentile   max value
aa     3.8             4
ab     3.9             4
ac     5               5
ad     2               2
ae     2               2

How can I do this?

my code:

 awk '{ item[$1]++; count[$1]+=$2; max[$1]=$2; percentile[$1,.9]=$2 } 
END{ for (var in item) print var,count[var],max[var],percentile[var] } '
  • 3
    What have you tried this far, and where in your code are you stuck? Best regards. – maulinglawns Nov 9 '16 at 8:04
  • @maulinglawns have managed to get the max and count, but not the percentile – Pradeep BS Nov 9 '16 at 10:08
  • @maulinglawns awk '{ item[$1]++; count[$1]+=$2; max[$1]=$2; percentile[$1,.9]=$2 } END{ for (var in item) print var,count[var],max[var],percentile[var] } ' – Pradeep BS Nov 9 '16 at 10:08
  • For percentiles, see stackoverflow.com/questions/24707705/… – rogerdpack Jan 15 '18 at 15:54
0

Using Miller:

$ cat my.dat | mlr --inidx --repifs --opprint stats1 -i -a p90,max -f 2 -g 1 \
  then label 'Item,90th percentile,max value'
Item 90th percentile max value
aa   3.800000        4
ab   3.900000        4
ac   5.000000        5
ad   2               2
ae   2               2

The --inidx is for positionally indexed input; --repifs since whitespace is repeated between columns; --opprint for tabular output. stats1 aggregating p90 and max (-a p90,max) on field 2 (-f 2) grouped by field 1 (-g 1); -i for interpolated percentiles. Then label can be used for custom column-renames.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.