Read lines by number from a large file

后端 未结 5 684
有刺的猬
有刺的猬 2020-12-13 02:44

I have a file with 15 million lines (will not fit in memory). I also have a small vector of line numbers - the lines that I want to extract.

How can I read-out the l

5条回答
  •  隐瞒了意图╮
    2020-12-13 03:37

    Before I was able to get an R solution/answer, I've done it in Ruby:

    #!/usr/bin/env ruby
    
    NUM_SEQS = 14024829
    
    linenumbers = (1..10).collect{(rand * NUM_SEQS).to_i}
    
    File.open("./data/uniprot_2011_02.tab") do |f|
      while line = f.gets
        print line if linenumbers.include? f.lineno 
      end
    end
    

    runs fast (as fast as my storage can read the file).

提交回复
热议问题