Search code examples
sedtailheadwc

Printing a line in a certain percentile of a larg text file


I am looking for a single line command to print a line in a certain percentile of a large text file. My preferred solution is something based on sed, wc -l, and/or head/tail as I already know how to do that with awk and wc -l. To make it more clear, if my file has 1K lines of text, I need to print for example the (95%*1K)th line of that file.


Solution

  • In bash:

    head -`echo scale=0\;$(cat file|wc -l)\*95/100 | bc -l` file | tail -n 1