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

2007/9/7, Bulhac M. [email protected]:

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

% 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)

From: Bulhac M. [mailto:[email protected]]

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

On Sep 7, 3:03 am, Bulhac M. [email protected] 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/.

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]

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.

2007/9/7, Bulhac M. [email protected]:

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

On Sep 7, 1:28 am, Peña, Botp [email protected] wrote:

irb(main):013:0> rio(‘test.txt’).lines[2…2]
=> [“3asdfasdf\n”]

kind regards -botp

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

On Sep 7, 4:50 am, “Robert K.” [email protected] wrote:

2007/9/7, Bulhac M. [email protected]:

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

2007/9/7, William J. [email protected]:

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

and low line numbers:

head -3 your_file | tail -1

:slight_smile:

robert

Alternatively to save processing after the match:

awk “NR == 3 {print; exit}” your_file

Felix

2007/9/7, rio4ruby [email protected]:

get first 4 lines (as always in ruby indexing starts at 0)

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. :slight_smile:

Kind regards

robert

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

On Sep 7, 10:30 am, “Robert K.” [email protected]
wrote:

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.

On 07.09.2007 18:22, William J. wrote:

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.

LOL

robert

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

line_three = File.readlines(the_file)[2]

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

James Edward G. II

From: rio4ruby [mailto:[email protected]]

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

On Sep 7, 8:34 pm, Peña, Botp [email protected] wrote:

From: rio4ruby [mailto:[email protected]]

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/ )

From: William J. [mailto:[email protected]]

> 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/ )

yap. very clean too, but,

  1. reads whole file in mem
  2. rescans array 2x

kind regards -botp

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs