I a having a sorted file with numeric values like
foo 2.3
bar 2.6
baz 4.7
and would like to have a one-liner which puts the percentile of a line into the last column, like
foo 2.3 0.3333
bar 2.6 0.6666
baz 4.7 1.0000
Thank you.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I assume you mean percent of lines, for this you need to know the number of lines first.
Here’s one way to do it as a two-pass solution with awk:
Output:
The first block is only active during
FNR == NRi.e. the first pass. The second block takes care of the printing.Other alternatives to determine length of file
Use
NR-1when starting second pass (FNR != NR):Use
wcbefore running awk: