Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unix command: how to get top n records

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?

like image 761
user3110379 Avatar asked Jun 16 '14 22:06

user3110379


1 Answers

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
like image 137
glenn jackman Avatar answered Nov 01 '22 21:11

glenn jackman