Robert Klemme
9/7/2007 1:19:00 PM
2007/9/7, rio4ruby <Christopher.Kleckner@gmail.com>:
> 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]
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