On Sep 7, 1:28 am, Pe?a, Botp <b... / delmonte-phil.com> wrote:
> From: Bulhac Mihai [mailto:mihai.bul... / yahoo.com]
> # how can i read only a line from a txt file?
> # for example i want to read only line 3
>
> if you want a ready-made solution (written in ruby, of course), you can use rio.
>
> irb(main):005:0> require 'rio'
> => true
>
> # get first 4 lines (as always in ruby indexing starts at 0)
>
> irb(main):006:0> rio('test.txt').lines[0..3]
> => ["1testing \n", "2testing \n", "3asdfasdf\n", "\n"]
>
> # get first lines 4 to 6
>
> irb(main):007:0> rio('test.txt').lines[3..5]       # a range of lines
> => ["\n", "4asdf\n", "[]\n"]
>
> # thus, reading line 3 would be
>
> irb(main):013:0> rio('test.txt').lines[2..2]
> => ["3asdfasdf\n"]
>
> kind regards -botp

Or even
  rio('test.txt').line[2]