I want to get top n records using unix command:
e.g. input:
output(get top 3):
Current I am doing:
cat myfile.txt | sort -k1nr | head -3 > my_output.txt
It works fine but when the file gets large, it becomes very slow.
It is slow because it sorts the file completely, while what I need is just the top 3 records.
Is there any command I can use to get the top 3 records?
perl -ane '
BEGIN {@top = ([-1]) x 3}
if ($F[0] > $top[0][0]) {
@top = sort {$a->[0] <=> $b->[0]} @top[1,2], [$F[0], $_];
}
END {print for reverse map {$_->[1]} @top}
' << END_DATA
1 a
2 b
3 c
4 d
5 e
END_DATA
5 e
4 d
3 c
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With