read a specific line from a file

B

Bulhac Mihai

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

Pierre-Charles David

2007/9/7 said:
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)
 
P

Peña, Botp

RnJvbTogQnVsaGFjIE1paGFpIFttYWlsdG86bWloYWkuYnVsaGFjQHlhaG9vLmNvbV0gDQojIGhv
dyBjYW4gaSByZWFkIG9ubHkgYSBsaW5lIGZyb20gYSB0eHQgZmlsZT8NCiMgZm9yIGV4YW1wbGUg
aSB3YW50IHRvIHJlYWQgb25seSBsaW5lIDMNCg0KeW91IGNhbiBvcGVuIHRoZSBmaWxlIGFuZCB0
aGVuIHJlYWQgZWFjaCBsaW5lLCBjb2xsZWN0aW5nIHRoZSBsaW5lcyB5b3Ugd2FudC4gZXhpdCBh
bnl0aW1lIGlmIHlvdSBoYXZlIHdoYXQgeW91IHdhbnQuDQoNCmlmIHlvdSB3YW50IGEgcmVhZHkt
bWFkZSBzb2x1dGlvbiAod3JpdHRlbiBpbiBydWJ5LCBvZiBjb3Vyc2UpLCB5b3UgY2FuIHVzZSBy
aW8uDQoNCmlyYihtYWluKTowMDU6MD4gcmVxdWlyZSAncmlvJw0KPT4gdHJ1ZQ0KDQojIGdldCBm
aXJzdCA0IGxpbmVzIChhcyBhbHdheXMgaW4gcnVieSBpbmRleGluZyBzdGFydHMgYXQgMCkNCg0K
aXJiKG1haW4pOjAwNjowPiByaW8oJ3Rlc3QudHh0JykubGluZXNbMC4uM10gDQo9PiBbIjF0ZXN0
aW5nIFxuIiwgIjJ0ZXN0aW5nIFxuIiwgIjNhc2RmYXNkZlxuIiwgIlxuIl0NCg0KIyBnZXQgZmly
c3QgbGluZXMgNCB0byA2DQoNCmlyYihtYWluKTowMDc6MD4gcmlvKCd0ZXN0LnR4dCcpLmxpbmVz
WzMuLjVdICAgICAgICMgYSByYW5nZSBvZiBsaW5lcw0KPT4gWyJcbiIsICI0YXNkZlxuIiwgIltd
XG4iXQ0KDQojIHRodXMsIHJlYWRpbmcgbGluZSAzIHdvdWxkIGJlDQoNCmlyYihtYWluKTowMTM6
MD4gcmlvKCd0ZXN0LnR4dCcpLmxpbmVzWzIuLjJdDQo9PiBbIjNhc2RmYXNkZlxuIl0NCg0Ka2lu
ZCByZWdhcmRzIC1ib3RwDQo=
 
E

expandafter

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

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]
 
R

rio4ruby

From: Bulhac Mihai [mailto:[email protected]]
# 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]
 
R

Robert Klemme

2007/9/7 said:
From: Bulhac Mihai [mailto:[email protected]]
# 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'
=3D> true

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

irb(main):006:0> rio('test.txt').lines[0..3]
=3D> ["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
=3D> ["\n", "4asdf\n", "[]\n"]

# thus, reading line 3 would be

irb(main):013:0> rio('test.txt').lines[2..2]
=3D> ["3asdfasdf\n"]

kind regards -botp

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

I know a simpler ready made solution:

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

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

Kind regards

robert
 
R

Robert Klemme

2007/9/7 said:
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

:)

robert
 
W

William James

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.
 
P

Peña, Botp

From: rio4ruby [mailto:[email protected]]=20
# 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=20
-botp
 
W

William James

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

Peña, Botp

From: William James [mailto:[email protected]]=20
# > rio has simple solutions for line grabbing, like eg, "get=20
# line 3..5, lines 100,200,300..400, and lines containing /test/"
# >
# > rio('test.txt').line[3..5,100,200,300..400,/test/]
#=20
# lines =3D IO.readlines("phrases_no_extra.txt")
# lines.values_at( 3..5,100,200,300..400 ) +
# lines.grep( /test/ )

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

kind regards -botp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,264
Messages
2,571,323
Members
48,006
Latest member
MelinaLema

Latest Threads

Top