I want to get top n records using unix command:
e.g. input:
- 1 a
- 2 b
- 3 c
- 4 d
- 5 e
output(get top 3):
- 5 e
- 4 d
- 3 c
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?