Q:

Bash - one liner to sort a bed file on qvalue column then extract top 20% of rows with highest q value

I have a bed file in the following format:
chr start   end q-value   name
chr1    10004  10467    310.43    peak_1
chr2    15410  15704    19.61 peak_2
chr3    21207  21354    4.04  peak_3
chr4    26073  26165    25.32 peak_4
chr5   63044057  63044425   39.65  peak_5

If possible, I need a bash one-liner to sort this file on the q-value column (column 4), then I need to extract the top 20% of rows with the highest q-value. 
After sorting this would look like:
chr start   end q-value   name
chr1    10004  10467    310.43    peak_1
chr5   63044057  63044425   39.65  peak_5
chr4   26073  26165    25.32 peak_4
chr2    15410  15704    19.61 peak_2
chr3    21207  21354    4.04  peak_3

After percentage it would look like:
chr1    10004  10467    310.43    peak_1

I need to run this on over 40 files.
I'm also familiar with R so if this is not possible in bash, but doable in R, R code would also be useful (but Bash is preferable).
Many Thanks.

Edit comments:
Made code more testable.
Re: my own attempt
When I tried to run sort -k4 file.txt in the first instance. I got the following which is not what I'm looking for:
chr2    15410  15704    19.61 peak_2
chr4    26073  26165    25.32 peak_4
chr1    10004  10467    310.43    peak_1
chr5   63044057  63044425   39.65  peak_5
chr3    21207  21354    4.04  peak_3

This confused me, I assume the decimals are causing an issue and not sure how to get round this first part.

A:

Is this what you are looking at?
#!/bin/sh
sort -r -g -k 4,4 < inputFile.file > tempfile_sorted.out
lncnt=$(wc -l < tempfile_sorted.out)
percent_linecount_infloat=$(echo "$lncnt*.2" | bc)
float2Int=$(printf %.0f "$percent_linecount_infloat")
head_20_percent=$(head -"$float2Int" tempfile_sorted.out)
new_fn=$(printf "%s_20" tempfile_sorted.out) # new file with top 20% of sorted output
printf "$head_20_percent" > $new_fn

