From: Robert Klemme Date: 2007-09-07T22:18:47+09:00 Subject: Re: read a specific line from a file 2007/9/7, rio4ruby : > On Sep 7, 1:28 am, Pe�a, Botp 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] I know a simpler ready made solution: ruby -ne 'puts $_ if $. == 3' your_file But I'd really prefer the sed solution. :-) Kind regards robert