Read a specific line from a file

how can i read only a line from a txt file?
for example i want to read only line 3

···

--
Posted via http://www.ruby-forum.com/.

% cat one_line.rb
#!/usr/bin/env ruby

def read_one_line(file_name, line_number)
  File.open(file_name) do |file|
    current_line = 1
    file.each_line do |line|
      return line if line_number == current_line
      current_line += 1
    end
  end
end

puts read_one_line(ARGV[0], ARGV[1].to_i)

% ruby one_line.rb one_line.rb 3
def read_one_line(file_name, line_number)

···

2007/9/7, Bulhac Mihai <mihai.bulhac@yahoo.com>:

how can i read only a line from a txt file?
for example i want to read only line 3

--

# how can i read only a line from a txt file?
# for example i want to read only line 3

you can open the file and then read each line, collecting the lines you want. exit anytime if you have what you want.

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

···

From: Bulhac Mihai [mailto:mihai.bulhac@yahoo.com]

ruby -e "puts ARGF.to_a[2]" myfile

Another way:

File.open("myfile"){|f|
  line = nil
  3.times { line = f.gets }
  puts line
}

If the whole file will fit in memory,
you can do this:

puts IO.readlines("myfile")[2]

···

On Sep 7, 3:03 am, Bulhac Mihai <mihai.bul...@yahoo.com> wrote:

how can i read only a line from a txt file?
for example i want to read only line 3
--
Posted viahttp://www.ruby-forum.com/.

sed -ne '3 p' your_file

robert

···

2007/9/7, Bulhac Mihai <mihai.bulhac@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 something quick and you're not going to reuse it in a bigger
program (bad, bad, use methods anyway! xD), you also can do it in one line,
such

line_three = File.readlines(the_file)[2] # File.readlines returns an array
containing all the lines of a file. Get the third element and you're done.

···

On 9/7/07, Peña, Botp <botp@delmonte-phil.com> wrote:

From: Bulhac Mihai [mailto:mihai.bulhac@yahoo.com]
# how can i read only a line from a txt file?
# for example i want to read only line 3

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

···

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

awk "3==NR" your_file

···

On Sep 7, 4:50 am, "Robert Klemme" <shortcut...@googlemail.com> wrote:

2007/9/7, Bulhac Mihai <mihai.bul...@yahoo.com>:

> how can i read only a line from a txt file?
> for example i want to read only line 3

sed -ne '3 p' your_file

robert

I know a simpler ready made solution:

ruby -ne 'puts $_ if $. == 3' your_file

But I'd really prefer the sed solution. :slight_smile:

Kind regards

robert

···

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]

When I think about it, this is probably more efficient for large files
and low line numbers:

head -3 your_file | tail -1

:slight_smile:

robert

···

2007/9/7, William James <w_a_x_man@yahoo.com>:

On Sep 7, 4:50 am, "Robert Klemme" <shortcut...@googlemail.com> wrote:
> 2007/9/7, Bulhac Mihai <mihai.bul...@yahoo.com>:
>
> > how can i read only a line from a txt file?
> > for example i want to read only line 3
>
> sed -ne '3 p' your_file
>
> robert

awk "3==NR" your_file

line_three = File.foreach(path) { |line| break line if $. == 3 }

James Edward Gray II

···

On Sep 7, 2007, at 4:00 AM, Diego Suarez wrote:

line_three = File.readlines(the_file)[2]

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

thanks. i just wanted (and hoped) that the op will try his hands on rio and experiment it.

rio has simple solutions for line grabbing, like eg, "get line 3..5, lines 100,200,300..400, and lines containing /test/"

rio('test.txt').line[3..5,100,200,300..400,/test/]

thanks for rio btw
-botp

···

From: rio4ruby [mailto:Christopher.Kleckner@gmail.com]

Alternatively to save processing after the match:

awk "NR == 3 {print; exit}" your_file

Felix

···

-----Original Message-----
From: Robert Klemme [mailto:shortcutter@googlemail.com]
Sent: Friday, September 07, 2007 8:31 AM
To: ruby-talk ML
Subject: Re: read a specific line from a file

2007/9/7, William James <w_a_x_man@yahoo.com>:
> On Sep 7, 4:50 am, "Robert Klemme" > <shortcut...@googlemail.com> wrote:
> > 2007/9/7, Bulhac Mihai <mihai.bul...@yahoo.com>:
> >
> > > how can i read only a line from a txt file?
> > > for example i want to read only line 3
> >
> > sed -ne '3 p' your_file
> >
> > robert
>
> awk "3==NR" your_file

When I think about it, this is probably more efficient for large files
and low line numbers:

head -3 your_file | tail -1

:slight_smile:

robert

I was curious:

$ time for loop in `seq 1 1000`; do awk "3==NR {print; exit}" lines >
/dev/null; done

real 0m1.819s
user 0m0.624s
sys 0m1.180s
$ time for loop in `seq 1 1000`; do head -3 lines | tail -1 > /dev/null;
done

real 0m3.427s
user 0m1.848s
sys 0m1.768s

Felix

···

-----Original Message-----
From: Robert Klemme [mailto:shortcutter@googlemail.com]
Sent: Friday, September 07, 2007 8:31 AM
To: ruby-talk ML
Subject: Re: read a specific line from a file

2007/9/7, William James <w_a_x_man@yahoo.com>:
> On Sep 7, 4:50 am, "Robert Klemme" > <shortcut...@googlemail.com> wrote:
> > 2007/9/7, Bulhac Mihai <mihai.bul...@yahoo.com>:
> >
> > > how can i read only a line from a txt file?
> > > for example i want to read only line 3
> >
> > sed -ne '3 p' your_file
> >
> > robert
>
> awk "3==NR" your_file

When I think about it, this is probably more efficient for large files
and low line numbers:

head -3 your_file | tail -1

:slight_smile:

robert

awk "3==NR{print;exit}" your_file

The rule is: never use anything other than awk unless
you have to.

···

On Sep 7, 10:30 am, "Robert Klemme" <shortcut...@googlemail.com> wrote:

2007/9/7, William James <w_a_x_...@yahoo.com>:

> On Sep 7, 4:50 am, "Robert Klemme" <shortcut...@googlemail.com> wrote:
> > 2007/9/7, Bulhac Mihai <mihai.bul...@yahoo.com>:

> > > how can i read only a line from a txt file?
> > > for example i want to read only line 3

> > sed -ne '3 p' your_file

> > robert

> awk "3==NR" your_file

When I think about it, this is probably more efficient for large files
and low line numbers:

head -3 your_file | tail -1

From: rio4ruby [mailto:Christopher.Kleck...@gmail.com]
# Or even
# rio('test.txt').line[2]

thanks. i just wanted (and hoped) that the op will try his hands on rio and experiment it.

He ought to learn and experiment with Ruby first.

rio has simple solutions for line grabbing, like eg, "get line 3..5, lines 100,200,300..400, and lines containing /test/"

rio('test.txt').line[3..5,100,200,300..400,/test/]

lines = IO.readlines("phrases_no_extra.txt")
lines.values_at( 3..5,100,200,300..400 ) +
  lines.grep( /test/ )

···

On Sep 7, 8:34 pm, Peña, Botp <b...@delmonte-phil.com> wrote:

LOL

  robert

···

On 07.09.2007 18:22, William James wrote:

On Sep 7, 10:30 am, "Robert Klemme" <shortcut...@googlemail.com> > wrote:

2007/9/7, William James <w_a_x_...@yahoo.com>:

On Sep 7, 4:50 am, "Robert Klemme" <shortcut...@googlemail.com> wrote:

2007/9/7, Bulhac Mihai <mihai.bul...@yahoo.com>:

how can i read only a line from a txt file?
for example i want to read only line 3

sed -ne '3 p' your_file
robert

awk "3==NR" your_file

When I think about it, this is probably more efficient for large files
and low line numbers:

head -3 your_file | tail -1

awk "3==NR{print;exit}" your_file

The rule is: never use anything other than awk unless
you have to.

# > rio has simple solutions for line grabbing, like eg, "get
# line 3..5, lines 100,200,300..400, and lines containing /test/"
# >
# > rio('test.txt').line[3..5,100,200,300..400,/test/]

···

From: William James [mailto:w_a_x_man@yahoo.com]
#
# lines = IO.readlines("phrases_no_extra.txt")
# lines.values_at( 3..5,100,200,300..400 ) +
# lines.grep( /test/ )

yap. very clean too, but,
1. reads whole file in mem
2. rescans array 2x

kind regards -botp